[英]KnockoutJs computed array not calculated correctly
The code is as follows: EmployeeModel is the viewModel and the problem is that when I change an item's property - deletedFlag in employees (obs array), deletedItems is not updated. 代码如下:EmployeeModel是viewModel,问题是当我更改项目的属性-雇员(obs数组)中的deleteFlag时,不会更新DeletedItems。
How can i fix this? 我怎样才能解决这个问题?
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);
}
EDIT: and the following code marks one item from the array for deletion 编辑:和以下代码将数组中的一项标记为删除
self.removeEmployee = function (employee) {
employee.deletedFlag(true);
};
The property deletedFlag
is an observable, therefore you need to retrieve its current value by invoking it as a function (you cannot compare it directly to any value): 属性
deletedFlag
是可观察的,因此您需要通过将其作为函数调用来检索其当前值(不能将其直接与任何值进行比较):
self.deletedItems = ko.computed(function () {
return ko.utils.arrayFilter(self.employees(), function (item) {
return item.deletedFlag() == true; // <===
});
}, this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.