I am using the click event on a button to set the value of an item that was generated using a foreach
.
我在按钮上使用click事件来设置使用foreach生成的项的值。
<table>
<tbody data-bind="foreach: Employees">
<a data-bind="click:$parent.delete()">
..
in my delete function I am setting the value but it doesn't update the screen
在我的删除功能中,我正在设置值,但它不会更新屏幕
Delete :(emp) {
emp.active=false;
}
When I create I am setting all the individual properties as observable but seems like they are not when in the foreach loop.
当我创建时,我将所有单个属性设置为可观察但看起来它们不是在foreach循环中。
- Update
- 更新
Employees is filtered.computed
员工被过滤。计算
var Employees=ko.computed(function() {
return ko.utils.arrayFilter(AllEmployees(), function (empid) {
return empid.ID == filter();
});
3 个解决方案
#1
3
When you get/set observables you need to call them like this:
获得/设置observable时,你需要像这样调用它们:
var val = obj.prop(); //Getter
obj.prop(false); //Setter
One other issue you have is that you are using parenthesis in your click binding. Remember that Knockout bindings are just javascript, so it will actually execute that expression when it binds.
另一个问题是你在点击绑定中使用括号。请记住,Knockout绑定只是javascript,因此它实际上会在绑定时执行该表达式。
You need to get rid of those parenthesis or emp
will be undefined initially.
你需要摆脱那些括号或者emp最初是不确定的。
UPDATE:
I've updated this jsFiddle to include three filtered lists similar to what you have shown above. You can see that using a filtered list via a computed has no bearing on how knockout handles the bindings, and the UI updates seamlessly.
我已经更新了这个jsFiddle以包含三个类似于上面显示的过滤列表。您可以看到,通过计算使用筛选列表与敲除如何处理绑定无关,并且UI无缝更新。
http://jsfiddle.net/jwcarroll/ceRPK/
http://jsfiddle.net/jwcarroll/ceRPK/
#2
1
To set an observable, you have to call it (since observables are implemented as functions):
要设置一个observable,你必须调用它(因为observable实现为函数):
emp.active(false);
Your method simply overwrites the observable.
您的方法只是覆盖了observable。
#3
0
Knockout subscribes to the observable array, but not to each observable within that array. If you want to subscribe to individual properties you need to subscribe manually by using myObservable.subscribe()
Knockout订阅了可观察数组,但没有订阅该数组中的每个observable。如果要订阅单个属性,则需要使用myObservable.subscribe()手动订阅
Knockout subscribes to the observable array, but not to each observable within that array. If you want to subscribe to individual properties you need to subscribe manually using myObservable.subscribe()
Knockout订阅了可观察数组,但没有订阅该数组中的每个observable。如果要订阅单个属性,则需要使用myObservable.subscribe()手动订阅
Edit
编辑
If you are trying to have your computed keep track of what should be in your computed you can do so like this -
如果你想让你的计算机跟踪你的计算中应该是什么,你可以这样做 -
var allEmployees = ko.observableArray([my data goes here]);
var Employees=ko.computed(function() {
return ko.utils.arrayFilter(allEmployees(), function (emp) {
return emp.active === true;
});
});
That works if active is not an observable property of each allEmployees(). If it is an observable just change that to -
如果active不是每个allEmployees()的可观察属性,则该方法有效。如果它是一个可观察的,只需改变 -
var allEmployees = ko.observableArray([my data goes here]);
var Employees=ko.computed(function() {
return ko.utils.arrayFilter(allEmployees(), function (emp) {
return emp.active();
});
});
#1
3
When you get/set observables you need to call them like this:
获得/设置observable时,你需要像这样调用它们:
var val = obj.prop(); //Getter
obj.prop(false); //Setter
One other issue you have is that you are using parenthesis in your click binding. Remember that Knockout bindings are just javascript, so it will actually execute that expression when it binds.
另一个问题是你在点击绑定中使用括号。请记住,Knockout绑定只是javascript,因此它实际上会在绑定时执行该表达式。
You need to get rid of those parenthesis or emp
will be undefined initially.
你需要摆脱那些括号或者emp最初是不确定的。
UPDATE:
I've updated this jsFiddle to include three filtered lists similar to what you have shown above. You can see that using a filtered list via a computed has no bearing on how knockout handles the bindings, and the UI updates seamlessly.
我已经更新了这个jsFiddle以包含三个类似于上面显示的过滤列表。您可以看到,通过计算使用筛选列表与敲除如何处理绑定无关,并且UI无缝更新。
http://jsfiddle.net/jwcarroll/ceRPK/
http://jsfiddle.net/jwcarroll/ceRPK/
#2
1
To set an observable, you have to call it (since observables are implemented as functions):
要设置一个observable,你必须调用它(因为observable实现为函数):
emp.active(false);
Your method simply overwrites the observable.
您的方法只是覆盖了observable。
#3
0
Knockout subscribes to the observable array, but not to each observable within that array. If you want to subscribe to individual properties you need to subscribe manually by using myObservable.subscribe()
Knockout订阅了可观察数组,但没有订阅该数组中的每个observable。如果要订阅单个属性,则需要使用myObservable.subscribe()手动订阅
Knockout subscribes to the observable array, but not to each observable within that array. If you want to subscribe to individual properties you need to subscribe manually using myObservable.subscribe()
Knockout订阅了可观察数组,但没有订阅该数组中的每个observable。如果要订阅单个属性,则需要使用myObservable.subscribe()手动订阅
Edit
编辑
If you are trying to have your computed keep track of what should be in your computed you can do so like this -
如果你想让你的计算机跟踪你的计算中应该是什么,你可以这样做 -
var allEmployees = ko.observableArray([my data goes here]);
var Employees=ko.computed(function() {
return ko.utils.arrayFilter(allEmployees(), function (emp) {
return emp.active === true;
});
});
That works if active is not an observable property of each allEmployees(). If it is an observable just change that to -
如果active不是每个allEmployees()的可观察属性,则该方法有效。如果它是一个可观察的,只需改变 -
var allEmployees = ko.observableArray([my data goes here]);
var Employees=ko.computed(function() {
return ko.utils.arrayFilter(allEmployees(), function (emp) {
return emp.active();
});
});