您好我需要构建动态表,其中包含来自数组的列(标题)和来自其他数组的行, 数组中的第一列必须是静态的。
<table>
<thead>
<tr>
<th>Static</th>
<th>Dynamic 1</th>
<th>Dynamic 2</th>
<th>Dynamic 3</th>
</tr>
</thead>
<tbody>
<tr>
<th>Nam1</th>
<th>value</th>
<th>value</th>
<th>value</th>
</tr>
</tbody>
</table>
我有下一个Knockout HTML模型allRoles这是带有动态标题的数组
<table>
<thead>
<tr data-bind="template: { name: 'tableHeader', foreach: allRoles, as: 'role',afterRender: addFirstColumn } ">
</tr>
</thead>
<tbody data-bind="foreach: {data: userRoles,as:'dep'}">
<tr>
<td>
<span data-bind="text: dep.name"></span>
</td>
<td data-bind="foreach: {data: dep.roles, as: 'role'}">
<span data-bind="text: role.id"></span>
</td>
</tr>
</tbody>
</table>
<script type="text/html" id="tableHeader">
<th data-bind="text: role.name">
</th>
</script>
我如何添加静态?
答案 0 :(得分:7)
您可能正在寻找Knockout的无容器控制流语法。它看起来像这样:
<table>
<thead>
<tr>
<th>Static</th>
<!-- ko foreach: allRoles -->
<th data-bind="text: name"></th>
<!-- /ko -->
</tr>
</thead>
</table>
请参阅注释4 :http://knockoutjs.com/documentation/foreach-binding.html
答案 1 :(得分:1)
将您的绑定更改为:
<thead>
<tr>
<th>Static</th>
<!-- ko template: { name: 'tableHeader', foreach: allRoles, as: 'role',afterRender: addFirstColumn } -->
<!-- /ko -->
</tr>
</thead>
这使用knockout描述为Virtual Element的东西来呈现你的foreach
绑定,而不需要父节点。