javascript - Handlebars add class -
i have following json:
"daysrange": { "status": "unspecified/all/specified", "available": [ { "id": "1", "name": "monday" }, { "id": "2", "name": "tuesday" }, { "id": "3", "name": "wednesday" }, { "id": "4", "name": "thursday" }, { "id": "5", "name": "friday" }, { "id": "6", "name": "saturday" }, { "id": "7", "name": "sunday" } ], "selected": [ { "id": "2", "name": "tuesday" } ] }
i'm using handlebars generate li's:
<ul class="available"> {{#each daysrange.available}} <li data-id="{{this.id}}">{{this.name}}</li> {{/each}} </ul>
question: how can add class selected selected days? i'll appreciate can shed light on this.
you need massage data before hits template. mark selected
day in available
list can test during iteration.
- parse json object
- iterate on
available
list , compare ids elements inselected
something like
if (available[i].id === selected[j].id) available[i].selected = true;`
you can custom helper, seems messier me correcting data:
handlebars.registerhelper('isselected', function(selected, options) { var ret = ""; if (this.id === selected[0].id) { ret = "selected"; } return options.fn(ret); });
then in template,
<ul class="available"> {{#each daysrange.available}} <li data-id="{{this.id}}" class="{{#isselected ../daysrange.selected}}{{this}}{{/isselected}}">{{this.name}}</li> {{/each}} </ul>
Comments
Post a Comment