[英]unable to create the handlebar template
我有一个带有JSON对象的车把模板。
处理条形码-
<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>
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 } ) );
像小提琴一样,此代码将为我正确呈现树结构数据-http: //jsfiddle.net/achyut/V6HNd/1/
现在,我的JSON对象已更改,并且现在有了额外的方括号,用于逻辑分隔。 我现在无法使用额外的括号来渲染树结构。 任何人都可以告诉需要在车把模板中更改哪些代码。
新的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" }
]
由于您具有项目数组,因此必须再次循环它并访问对象,您可以使用this
{{#each items}}
{{#each this}} {{! Each item is an "li" }}
{{! Code here }}
{{/each}}
{{/each}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.