Meteor Dropdown list get and set
Asked Answered
S

2

6

What is the best way to get and select values from a dropdown list (and also in radio) in Meteor. I have created a helper:

Template.categories.helpers({
    categories: ["facebook", "news", "tv", "tweets"]
});

and in html

...
<select class="form-control" id="category">
  {{> categories}}
</select>
...
<template name="categories">
  <option disabled="disabled" selected="selected">Please Select</option> 
    {{#each categories}}
      <option value="{{this}}">{{this}}</option>
    {{/each}}
</template>

In case of edit, I would like to evaluate it with value coming from database (e.g. news) to be selected.

Thanks in advance.

Sherrisherrie answered 15/2, 2015 at 17:7 Comment(1)
I have answered the best I can without knowing the details about what is being returned from you database. Can you give us more detail? What you usually have to do is have an additional helper in each option that will compare two values; if they match you return "selected".Sherry
S
10

Template HTML:

<select id="category-select">
    <option disabled="disabled" selected="selected">Please Select</option> 
    {{#each categories}}
        <option value="{{this}}">{{this}}</option>
    {{/each}}
</select>

Template js:

Template.categories.helpers({
    categories: function(){
        return ["facebook", "news", "tv", "tweets"]
    }
});

Template.categories.events({
    "change #category-select": function (event, template) {
        var category = $(event.currentTarget).val();
        console.log("category : " + category);
        // additional code to do what you want with the category
    }
});
Sherry answered 15/2, 2015 at 18:16 Comment(0)
C
1
Template.categories.helpers({
    categories: function(){
        return ["facebook", "news", "tv", "tweets"]
    }
});

And you should consider changing template name and helper, they shouldn't be the same.

Contiguity answered 15/2, 2015 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.