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.

  1. parse json object
  2. iterate on available list , compare ids elements in selected
  3. 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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -