我在每行数据中有一个全选切换复选框和复选框。
现在从服务器返回的数据没有isSelected
Observable。我为每一行添加了 'isSelected' observable。但是isSelected
observable 不会绑定到每行中的复选框。
这是视图模型:
var folderViewModel = function () {
var self = this;
self.Folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
self.SelectedFolder = ko.observable();
self.Mails = ko.observableArray([]);
self.SelectedMail = ko.observable();
self.SelectAll = ko.observable(false);
self.navigate = function (folder) {
self.SelectedFolder(folder);
//$.get('/Api/MailBox', { folder: folder }, self.Mails);
$.ajax({
url: "/Api/Mailbox",
data: { folder: folder },
success: function (data) {
ko.mapping.fromJS(data, {}, self.Mails);
ko.utils.arrayForEach(self.Mails(), function (mail) {
mail.isSelected = ko.observable(true);
mail.isSelected.subscribe(function (myvalue) {
console.log(myvalue);
});
});
console.log(ko.toJSON(self.Mails()));
},
statusCode: {
404: function () {
alert("No Mail");
}
}
});
//ko.mapping.fromJS(data, {}, self.Mails);
//console.log(ko.toJSON(self.Mails));
};
self.SelectAll.subscribe(function (newValue) {
ko.utils.arrayForEach(self.Mails(), function (mail) {
console.log(mail.isSelected());
mail.isSelected(newValue);
});
console.log(newValue);
}, self);
this.navigate("Inbox");
};
ko.applyBindings(new folderViewModel());
这是绑定。
<table class="table table-bordered table-striped table-condensed table-hover">
<thead>
<tr>
<th>
<input type="checkbox" data-bind="checked: SelectAll"/>
@*<input type="checkbox" />*@
</th>
<th>
From
</th>
<th>
To
</th>
<th>
Subject
</th>
<th>
Date
</th>
</tr>
</thead>
<tbody data-bind="foreach:Mails">
<tr data-bind="click:$root.navigateToMail">
<td style="width: 15px">
<input type="checkbox" data-bind="checked: $root.isSelected">
@*<input type="checkbox">*@
</td>
<td data-bind="text: From">
</td>
<td data-bind="text: To">
</td>
<td data-bind="text: Subject">
</td>
<td data-bind="text: MailDate">
</td>
</tr>
</tbody>
该复选框 <input type="checkbox" data-bind="checked: $root.isSelected">
未绑定到mails.isSelected=ko.obsevable(true)
. 可能是什么问题?