我可以在Handlebars中遍历多个数组吗?

时间:2013-03-18 22:42:53

标签: jquery handlebars.js

这里有点高,但我想把这个数组/设置传递给把手

  var thisWeek = {
    date: "18-3-13",
    comment: "Run through days of the week",
    days:[{
    monday:[
      {vehicle: "Number 1", driver: "Jack_Franklin", events: "Boat Race"},
      {vehicle: "Number 2", driver: "Paul Smith", events: "Racing"},
      {vehicle: "Number 3", driver: "Andrew Kay", events: "Skiing"} 
      ],
    tuesday:[
      {vehicle: "Number 1", driver: "Jack_Franklin", events: "pick up trailer"},
      {vehicle: "Number 2", driver: "Paul Smith", events: "MOT "},
      {vehicle: "Number 3", driver: "Andrew Kay", events: "Park up for day"} 
      ],
    }],
  }

我想要做的是迭代一周的日子并列出当天的所有车辆,例如 -

    <script id="ajax-comment" type="text/x-handlebars-template">

    {{#each days}}
    <h2> {{ The day of the week ie Monday - Tuesday etc}}  </h2> 

           {{# each vehicle in monday etc }} 
             {{vehicle}}: {{driver}} : {{events}} <br/>
           {{/each }} 

    {{/each }}  

    </script>

我可以通过星期一的细节来传递一个数组,这对我有用但无论如何我可以立刻做到这一点吗?理想情况下我想在20天左右通过每天15辆车的细节..希望这有道理吗? ,有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

...假设您可以将结构更改为:

{
    days: [{
        day: "Monday",
        vehicles: [{vehicle: "Number 1", driver: "Jack_Franklin", events: "pick up trailer"}]
    }, {
        day: "Tuesday",
        vehicles: [{vehicle: "Number 1", driver: "Jack_Franklin", events: "pick up trailer"}]
    }]
}

......你应该能够这样做;

{{#each days}}
    <h2>The day of the week is {{day}} </h2>

    {{#each vehicles}}
        {{vehicle}}: {{driver}} : {{events}} <br />
    {{/each}}
{{/each}}