Javascript 无法创建把手模板
我有一个handlebar模板,它接受一个JSON对象 手柄条码-Javascript 无法创建把手模板,javascript,backbone.js,handlebars.js,require-handlebars,Javascript,Backbone.js,Handlebars.js,Require Handlebars,我有一个handlebar模板,它接受一个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
<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}}
这段代码将为我正确地呈现树结构数据,就像在小提琴中一样-
现在我的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}}
所以
项目
现在是[[[{…},…]]
而不是[{…},…]
?你为什么不对数据进行预处理,使其再次变得合理?只需在项目中剥离一层数组即可。是的,你说得对。关于剥离,我实际上无法做到这一点,因为我实际上得到了一个非常大的负载,其中项目只是其中一个对象,我有一个非常大的手柄模板来呈现整个页面。这只是我分享的一小部分。我无法为这个小问题写出不同的逻辑chunk@muistooshort是否无法单独重写handlebar代码来呈现页面
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" }
]
{{#each items}}
{{#each this}} {{! Each item is an "li" }}
{{! Code here }}
{{/each}}
{{/each}}