我有一个带有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" }
]
答案 0 :(得分:0)
由于你有多个项目,你必须再次循环它并访问你可以使用的对象this
{{#each items}}
{{#each this}} {{! Each item is an "li" }}
{{! Code here }}
{{/each}}
{{/each}}