编辑功能在敲除js中不起作用 [英] edit functionality is not working in knockout js
问题描述
我刚刚使用net.net上的示例创建了一个可编辑的对象,当我尝试编辑网格时,它没有显示值,而是将输入字段显示为空白.有什么帮助吗? 在编辑中,我正在调用此功能
I just created an editable using an example available on net.in this when i am trying to edit the grid then it doesn't show the value it shows the input field blank.can any body help ? in edit i am calling this function
self.editFruit = function (fruit) {
if (self.editingItem() == null) {
// start the transaction
fruit.beginEdit(self.editTransaction);
// shows the edit fields
self.editingItem(fruit);
}
};
这是小提琴 jsfiddle
推荐答案
在第一次评估绑定时,每个水果的可观察值(data-bind="value: name.editValue"
)的editValue子可观察值都不存在.当您单击编辑"链接时,将创建可观察到的editValue,但敲除并不知道必须重新绑定.
At the time the binding is evaluated for the first time the editValue child observable of each of fruit's observables ( data-bind="value: name.editValue"
) doesn't exist. When you click on the "edit" link the editValue observable is created but knockout doesn't know that it has to rebind.
您可以解决这两种方法.
You can solve this 2 ways.
1.在每个输入周围创建一个虚拟的if
绑定.如果if变为true,则内容将重新插入到DOM中,从而导致绑定重新评估.在设置了editItem observable之前,请确保将editValue observable附加到其父项上,否则情况相同
1 . Create a virtual if
binding around each input. When the if becomes true, the content will be reinserted back into the DOM causing the bindings to re-evaluate. Make sure that editValue observable is attached to its parent BEFORE editingItem observable is set, otherwise you are in the same situation
<!-- ko if: $root.isItemEditing($data) -->
<input data-bind="..."></input>
<!-- /ko -->
2.在绑定模型之前,请确保所有可观察对象的父对象可观察对象都具有editValue可观察对象,并在beginEdit fn中设置了editValue可观察对象的值.
2 . Make sure that all observables have the editValue observable attached to the parent observable before the model is bound, the set editValue observable's value in the beginEdit fn.
function Fruit(data) {
var self = this;
self.name = ko.observable(data.name || "");
self.name.editValue = ko.observable();
self.rate = ko.observable(data.rate || "");
self.rate.editValue = ko.observable();
}
ko.observable.fn.beginEdit = function (transaction) {
...
if (self.slice)
self.editValue(self.slice());
else
self.editValue(self());
...
}
这篇关于编辑功能在敲除js中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!