So this is the general gist of my data (copied the look from chrome webkit inspector).
> Object
> Fruit: Array[2]
> 0: Object
name: "banana"
color: "yellow"
animalthateats: "monkey"
id: 39480
> 1: Object
length: 2
> Vegetables: Array[179]
> Dairy: Array[2]
> Meat: Array[3]
> __proto__: Object
And this is what I want to do (in general):
<select>
<option value="">All Shows (default)</option>
<optgroup label="Fruit">
<option value="39480">Banana</option>
<option value="43432">Strawberry</option>
</optgroup>
<optgroup label="Vegetables">
<option value="3432">Broccoli</option>
</optgroup>
</select>
I'm sorta new to the whole templating thing, and it definitely seems non-straightforward to accomplish... if I can use jQuery anyway that will work too, but preferably just with this setup.
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.
use just "this"
`<script id="some-template" type="text/x-handlebars-template">
<option value="none">Selec</option>
{{#each data}}
<optgroup label="{{@key}}">
{{#each this}}
<option value="{{id}}">{{name}}</option>
{{/each}}
</optgroup>
{{/each}}
</script>`
http://jsfiddle.net/rcondori/jfav4o6u/
Your current data format presents you with two problems:
You'll have better luck if you can rearrange your data to be nested arrays, something like this:
var foods = { /* what you already have */ };
var for_hb = [
{ name: 'Fruit', foods: foods.Fruit },
{ name: 'Vegetables', foods: foods.Vegetables },
//...
];
You can do that with something simple like this:
var for_hb = [ ];
for(var k in foods)
for_hb.push({name: k, foods: foods[k]});
for_hb.sort(function(a, b) {
a = a.name.toLowerCase();
b = b.name.toLowerCase();
return a < b ? -1
: a > b ? +1
: 0;
});
var html = compiled_template({ groups: for_hb });
Then your template is simple:
<select>
<option value="">All Shows (default)</option>
{{#each group}}
<optgroup label="{{name}}">
{{#each foods}}
<option value="{{id}}">{{name}}</option>
{{/each}}
{{/each}}
</select>
You could write a helper to iterate over an object but you'd still have to specify the keys in an array if you wanted to be sure of a sensible display order.
You can do this via a custom component see example, this is not supported by our default {{each}}
helper (and that is intentional).
Sample Data:
a = {a:'muhammad', b :'asif', c: 'javed', username: 'maxifjaved'}
**
Online Demo for iterate throw an Object
http://emberjs.jsbin.com/yuheke/1/edit?html,js,output
**
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With