简体   繁体   English

无法创建车把模板

[英]unable to create the handlebar template

I am having a handlebar template which takes a JSON object. 我有一个带有JSON对象的车把模板。

Handle Bar Code - 处理条形码-

<script id="list" type="x-handlebars-template">
{{#each items}} {{! Each item is an "li" }}
<li>
    {{label}} :
    {{value}}
    {{#if items}}
    <ul>
    {{> list}} {{! Recursively render the partial }}
    </ul>
    {{/if}}
</li>
{{/each}}

<script id="main" type="x-handlebars-template">
    <ul>
        {{> list}}
    </ul>
</script>

The JSON object as - JSON对象为-

var items =[
  { "label": "TotalRented", "value": "5" },
  { "label": "AncilleryItems", "items": [
    { "label": "Item", "value": "CarSeat" },
    { "label": "Qty", "value": "2" },
    { "label": "Cost","items": [
          { "label": "VAT", "value": "$20" },
          { "label": "Total", "value": "$100" }
      ]
    },
    { "label": "Item", "value": "GPS" },
    { "label": "Qty", "value": "1" },
    { "label": "Cost", "items": [
          { "label": "VAT", "value": "$10" },
          { "label": "Total", "value": "$50" }
      ]
    }
  ]
  },
  { "label": "Colour", "value": "red" }
 ];

// The main template.
var main = Handlebars.compile( $( "#main" ).html() );

// Register the list partial that "main" uses.
Handlebars.registerPartial( "list", $( "#list" ).html() );

// Render the list.
$( "body" ).html( main( { items: items } ) );

This code would render the tree structure data properly for me as in the fiddle - http://jsfiddle.net/achyut/V6HNd/1/ 像小提琴一样,此代码将为我正确呈现树结构数据-http: //jsfiddle.net/achyut/V6HNd/1/

Now my JSON object has been changed and it now has extra square brackets, for logical seperation. 现在,我的JSON对象已更改,并且现在有了额外的方括号,用于逻辑分隔。 I am not able to render the tree structure with extra brackets now. 我现在无法使用额外的括号来渲染树结构。 Can anyone tell what code needs to be changed in the handlebar template. 任何人都可以告诉需要在车把模板中更改哪些代码。

The new JSON object is 新的JSON对象是

var items = [
  { "label": "TotalRented", "value": "5" },
  { "label": "AncilleryItems", "items": [
  [
    { "label": "Item", "value": "CarSeat" },
    { "label": "Qty", "value": "2" },
    { "label": "Cost","items": [
        [
          { "label": "VAT", "value": "$20" },
          { "label": "Total", "value": "$100" }
        ]
      ]
    }
  ],
  [
    { "label": "Item", "value": "GPS" },
    { "label": "Qty", "value": "1" },
    { "label": "Cost", "items": [
        [
          { "label": "VAT", "value": "$10" },
          { "label": "Total", "value": "$50" }
        ]
      ]
    }
  ]
 ]
},
{ "label": "Colour", "value": "red" }
]

Since you have array of items, you have to loop it again and to access the object you can use this 由于您具有项目数组,因此必须再次循环它并访问对象,您可以使用this

{{#each items}} 
   {{#each this}} {{! Each item is an "li" }}

     {{! Code here }}

   {{/each}}
{{/each}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM