1

Newbie to Node.js here:

What is an easy way to make dynamic dropdowns in node.js?

Basically, I have two tables: Skill and Skill_Category .

I want to select from the Skill_Category and be presented with the associated Skill.

I would assume I would need to use some template engine (to render in a webpage).

I tried researching a good amount on google, but didn't turn up anything that helped me.

If anyone can point me in the right direction?

Thank you!

5
  • You can use any dropdown library out there to run client side, why do you want it to be created by your templating engine? And what exactly do you mean by a "dynamic" dropdown? Commented Oct 6, 2014 at 14:26
  • So, let me clarify: It doesn't need to be created by a template engine. By "Dynamic Dropdown", I'm saying "Dynamic" because the first selection dropdown from [Skill_Category] will determine what is displayed in [Skill] dropdown - does that make sense? Commented Oct 6, 2014 at 14:31
  • thanks, let me think about it for a moment and I'll draft up an answer Commented Oct 6, 2014 at 14:32
  • Are you planning on using any front end libraries like jquery or angular? Commented Oct 6, 2014 at 14:33
  • most likely, I'm ultimately trying to build a barebones version of something like imgur.com/8lDPb1N Commented Oct 6, 2014 at 14:37

1 Answer 1

3

So one dropdown will cause the other to show. Unless the second dropdown has hundreds of options you won't want to make a separate server side request to get it in. This means your logic should all be in the browser/client side.

This means you want your "Skill_Category" select box to have a JS function called to hide the current "Skill" select box and show the newly chosen "Skill" select box. They will all be rendered in html by your templating, but only one will ever be shown (display:block) while the others are hidden (display:none).

Sample HTML:

 <select id="skill_category">
   <option value="communication">Communication</option>
   <option value="teamwork">Team Work</option>
   <option value="technical">Technical</option>
 </select> 


   <select class="skill" id="communciation">
     <option value="1">One</option>
     <option value="2">Two</option>
     <option value="3">Three</option>
   </select> 
   <select  class="skill" id="teamwork">
     <option value="1">One</option>
     <option value="2">Two</option>
     <option value="3">Three</option>
   </select> 
   <select  class="skill" id="technical">
     <option value="1">One</option>
     <option value="2">Two</option>
     <option value="3">Three</option>
   </select> 

Sample Jquery code:

$('#skill_category').on('change',function(){
  $('.skill').hide()
  $('#'+this.value).show()
});

Update:

If you have a large list of options for the secondary select box then you can make an ajax (HTTP GET) request, and returning the lsit of dropdowns from your server as JSON. You will probably have one of two scenarios: all of you skills in a static JSON file, or saved in a database.

Your node code will look like:

app.get('/skill_category/:skill', function(req, res){
  //JSON file in the /public/skills directory
  res.sendFile(__dirname + '/public/skills/'+req.params.skill+".json");

  //or some database lookup followed by a json send:
  var skills = someDatabaseLookup();
  res.json(skills);
 });

HTML

 <select id="skill_category">
   <option value="communication">Communication</option>
   <option value="teamwork">Team Work</option>
   <option value="technical">Technical</option>
 </select> 
 <select id="skill">
 </select> 

while the jquery will now be:

$('#skill_category').on('change',function(){
  $.get('skill_category/'+this.value,function(data){
     
     for(var j = 0; j < length; j++)
     {

       $('#skill').contents().remove();
       var newOption = $('<option/>');
       newOption.attr('text', data[j].text);
       newOption.attr('value', data[j].value);
       $('#skill').append(newOption);
     }
  });
});

Do note this code is untested but should work

Sign up to request clarification or add additional context in comments.

2 Comments

I'll likely have hundreds of [skills] though , how can I do this more dynamic instead of loading all at once and hiding some?
this is probably the most dynamic function since it will be the most responsive. You can look to implementing a Single Page Application with frameworks like Angular or React to further bind your data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.