0

Trying to dynamically create a select input with options from a json array of objects. The JSON array of objects looks like this:

var curr_sel = [
    {v:"1", n:"USD"},
    {v:"2", n:"GBP"},
    {v:"3", n:"CAD"},
    {v:"4", n:"AUD"},
    {v:"5", n:"EUR"}
];

And my view template has the following:

<select>
{{#each opt in curr_sel}}
    <option value="{{ opt.v }}" >{{ opt.n }}</option>
{{/each}}
</select>

But that produces a select element with no options. Kindly help. Even better if it means using the EmberJS select class http://emberjs.com/api/classes/Ember.Select.html

1 Answer 1

1

The better way to use the select tag is using Ember.Select.

Here is a sample:

Template

<script type="text/x-handlebars" data-template-name="application">   
  {{view Ember.Select content=curr_sel
    optionLabelPath="content.n"
    optionValuePath="content.v"
    selection=selectedCurrency}}
</script>

Javascript

App = Ember.Application.create({});

App.ApplicationController = Ember.Controller.extend({
    curr_sel: [
        {v:"1", n:"USD"},
        {v:"2", n:"GBP"},
        {v:"3", n:"CAD"},
        {v:"4", n:"AUD"},
        {v:"5", n:"EUR"}
    ],
    selectedCurrency: null,
    selectedCurrencyChanged: function() {
        console.log(this.get('selectedCurrency.n'));
    }.observes('selectedCurrency')
});

Give a look in this fiddle to see this in action http://jsfiddle.net/marciojunior/FJkEr/

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

Comments

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.