简体   繁体   English

编辑功能在敲除js中不起作用

[英]edit functionality is not working in knockout js

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 ? 我只是使用net.in上提供的示例创建了一个可编辑内容,当我尝试编辑网格时,它没有显示值,而是将输入字段显示为空白。有什么帮助吗? 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);
        }
    };

here is fiddle jsfiddle 这里是小提琴的jsfiddle

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. 在第一次评估绑定时,每个水果的可观察值( data-bind="value: name.editValue" )的editValue子可观察data-bind="value: name.editValue"不存在。 When you click on the "edit" link the editValue observable is created but knockout doesn't know that it has to rebind. 当您单击“编辑”链接时,将创建可观察的editValue,但敲除并不知道必须重新绑定。

You can solve this 2 ways. 您可以解决这两种方法。

1 . 1。 Create a virtual if binding around each input. if在每个输入周围绑定, if创建一个虚拟的。 When the if becomes true, the content will be reinserted back into the DOM causing the bindings to re-evaluate. 如果if变为true,则内容将重新插入到DOM中,从而导致绑定重新评估。 Make sure that editValue observable is attached to its parent BEFORE editingItem observable is set, otherwise you are in the same situation 在设置了editItem observable之前,请确保将editValue observable附加到其父项,否则您将处于相同情况

<!-- ko if: $root.isItemEditing($data) -->
<input data-bind="..."></input>
<!-- /ko -->

2 . 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. 在绑定模型之前,请确保所有可观察对象的父对象可观察对象都具有editValue可观察对象,并且在beginEdit fn中设置了editValue可观察对象的值。

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());

  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM