這與本主題中的其他問題類似,但不同。knockoutjs在取消選中一個或多個項目時取消選擇/選中所有複選框
我有一個記錄列表,每個記錄都有一個select複選框。
在表頭中我有一個「全選」複選框。
當用戶選中/取消選中「全選」時,記錄被選中/取消選中。這工作正常。
但是,當一個或多個記錄被取消選擇時,我需要取消選擇我的「全選」複選框。
我的標記:
<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);
}
這就是現貨。非常感謝你。 – Wazygoose 2013-02-20 14:20:25
@Anders我使用沒有「Selected」列的Json對象綁定數據。通過Json對象綁定數據時的任何替代方法? – seadrag0n 2014-07-21 14:34:34
您需要創建一個ViewModel,用於保存數據旁邊的信息。 – Anders 2014-07-21 16:03:31