代码如下:EmployeeModel 是 viewModel,问题是当我更改项目的属性 - employees(obs 数组)中的 deletedFlag 时,deletedItems 没有更新。
我该如何解决这个问题?
function Employee(data) {
this.employeid = ko.observable(data.employeid);
this.name = ko.observable(data.name);
this.isactive = ko.observable(data.isactive);
this.deletedFlag = ko.observable(false);
}
var EmployeeModel = function () {
var self = this;
self.employees = ko.observableArray([]);
self.deletedItems = ko.computed(function () {
return ko.utils.arrayFilter(self.employees(), function (item) {
return item.deletedFlag == true;
});
}, this);
}
编辑:下面的代码将数组中的一项标记为删除
self.removeEmployee = function (employee) {
employee.deletedFlag(true);
};
最佳答案
属性 deletedFlag
是一个可观察对象,因此您需要通过将其作为函数调用来检索其当前值(您不能将其直接与任何值进行比较):
self.deletedItems = ko.computed(function () {
return ko.utils.arrayFilter(self.employees(), function (item) {
return item.deletedFlag() == true; // <===
});
}, this);
关于javascript - KnockoutJs 计算数组计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12890601/