【发布时间】:2013-02-20 12:38:56
【问题描述】:
这与围绕该主题的其他问题类似,但又有所不同。
我有一个包含记录列表的表格,每个记录都有一个选择复选框。
在表格标题中,我有一个“全选”复选框。
当用户选中/取消选中“全选”时,记录被选中/取消选中。这很好用。
但是,当取消选择一条或多条记录时,我需要取消选择“全选”复选框。
我的标记:
<table>
<thead>
<tr>
<th>Name</th>
<th><input type="checkbox" data-bind="checked: SelectAll" /></th>
</tr>
</thead>
<tbody data-bind="foreach: $data.People">
<tr>
<td data-bind="text: Name"></td>
<td class="center"><input type="checkbox" data-bind="checked: Selected" /></td>
</tr>
</tbody>
</table>
我的脚本(已编辑):
function MasterViewModel() {
var self = this;
self.People = ko.observableArray();
self.SelectAll = ko.observable(false);
self.SelectAll.subscribe(function (newValue) {
ko.utils.arrayForEach(self.People(), function (person) {
person.Selected(newValue);
});
});
}
my.Person = function (name, selected) {
var self = this;
self.Name = name;
self.Selected = ko.observable(false);
}
【问题讨论】:
标签: javascript checkbox knockout.js selectall