传入的数据是这样的 [[1,2,3],[4,5,6]]
有时是这样的 [[1,2],[ 4,5]]
。这是 HTML。
<button data-bind="click: refreshJSON">Test</button>
<table>
<tbody data-bind="foreach: array">
<tr data-bind="foreach: subarray">
<td data-bind="text: $data"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
var ViewModel = {
tableModel : ko.observableArray([[1,2,3],[4,5,6]]),
refreshJSON : function(){
this.tableModel([[1,2],[4,5]]);
}
};
ko.applyBindings(ViewModel);
</script>
我猜我必须在每个子数组上使用 ko.observableArray()
但是我不清楚如何执行此操作或如何执行 数据绑定(bind)
在 HTML 中。
最佳答案
更新:删除了 observableArray 位中的 observableArray。显然那是行不通的。只需绑定(bind)您的外部集合即可。
绑定(bind)部分相当简单:
<div data-bind="foreach: tableModel">
<div data-bind="foreach: $data">
<span data-bind="text: $data"></span>
</div>
</div>
关于knockout.js - 阵列的 knockout 阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902224/