Handlebars js,获取特定对象数组并循环遍历
Handlebars js, get specific object array and loop through
我正在尝试从一个数组中生成两个表。
"tables": [
{
"tableName": "table1",
"dataRow": ["name": "test858"]
},
{
"tableName": "table2",
"anotherRow": ["name": "test123"]
}
]
我试过这样做
{{#each tables}}
<table>
<thead>
<tr>
<th>{{tableName}}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
{{#if dataRow}}
<td>test1</td>
{{else}}
<td>test2</td>
{{/if}}
</tr>
</tbody>
</table>
{{/each}}
这会打印两个表名和 "test2"
如何遍历两个数组?
工作示例JSFiddle:
var entry_template = document.getElementById('entry-template').innerHTML;
var tables = [
{
"tableName": "table1",
"dataRow": [{"name": "test858"}]
},
{
"tableName": "table2",
"anotherRow": [{"name": "test123"}]
}
];
var template = Handlebars.compile(entry_template);
var template_with_data = template(tables);
document.getElementById('data').insertAdjacentHTML('afterbegin', template_with_data);
模板:
<script id="entry-template" type="text/x-handlebars-template">
{{#each this}}
<table>
<thead>
<tr>
<th>{{tableName}}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
{{#if dataRow}}
<td>test1</td>
{{else}}
<td>test2</td>
{{/if}}
</tr>
</tbody>
</table>
{{/each}}
</script>
<div id="data">
</div>
我正在尝试从一个数组中生成两个表。
"tables": [
{
"tableName": "table1",
"dataRow": ["name": "test858"]
},
{
"tableName": "table2",
"anotherRow": ["name": "test123"]
}
]
我试过这样做
{{#each tables}}
<table>
<thead>
<tr>
<th>{{tableName}}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
{{#if dataRow}}
<td>test1</td>
{{else}}
<td>test2</td>
{{/if}}
</tr>
</tbody>
</table>
{{/each}}
这会打印两个表名和 "test2"
如何遍历两个数组?
工作示例JSFiddle:
var entry_template = document.getElementById('entry-template').innerHTML;
var tables = [
{
"tableName": "table1",
"dataRow": [{"name": "test858"}]
},
{
"tableName": "table2",
"anotherRow": [{"name": "test123"}]
}
];
var template = Handlebars.compile(entry_template);
var template_with_data = template(tables);
document.getElementById('data').insertAdjacentHTML('afterbegin', template_with_data);
模板:
<script id="entry-template" type="text/x-handlebars-template">
{{#each this}}
<table>
<thead>
<tr>
<th>{{tableName}}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
{{#if dataRow}}
<td>test1</td>
{{else}}
<td>test2</td>
{{/if}}
</tr>
</tbody>
</table>
{{/each}}
</script>
<div id="data">
</div>