【发布时间】:2014-02-23 00:10:53
【问题描述】:
几个小时以来,我一直在与看起来应该是一个非常简单的 Knockout.js 任务作斗争,在阅读了多个关于 SO 的网站和问题之后,我仍然感到困惑!
这是小提琴: http://jsfiddle.net/PL8UW/
基本上我有一个表,其中包含一个绑定到淘汰视图模型的“状态”列。
我还有一个可观察对象,可以让您过滤各种状态
如果我将表直接绑定到数据对象,一切都会显示,但如果我绑定到计算,则什么都不会显示。
HTML:
<form>
<input id="foo" type="checkbox" checked /> Foo
<input id="bar" type="checkbox" checked /> Bar
</form>
<div id="tableDiv">
<table>
<thead>
<tr>
<th>Id</th>
<th>Status</th>
</tr>
</thead>
<tbody data-bind="foreach: filteredData">
<tr>
<td><span data-bind="html: id"></span></td>
<td><span data-bind="html: status"></span></td>
</tr>
</tbody>
</table>
</div>
javascript: 变量数据 = [ {“id”:1,“状态”:“foo”}, {“id”:2,“状态”:“栏”}, {“id”:3,“状态”:“foo”}, {“id”:4,“状态”:“foo”}, {"id": 5, "status": "bar"}];
var viewModel = {
tableData: ko.observableArray(data),
filter: ko.observable({'foo': ko.observable(true), 'bar': ko.observable(true)}),
filteredData: ko.computed(function() {
return ko.utils.arrayFilter(viewModel.tableData, function(item) {
return viewModel.filter[item.Status];
});
})
};
ko.applyBindings(viewModel, document.getElementById('tableDiv'));
$('input[type=checkbox]').click(function () {
viewModel.filter[this.id] = this.checked;
});
【问题讨论】:
标签: javascript knockout.js binding