简体   繁体   English

如何在敲除绑定处理程序中更新可观察数组?

[英]How do i update observable array in knockout binding handler?

I want to update the observable array in knockout binding handler. 我想在敲除绑定处理程序中更新可观察数组。 But it is not updating. 但是它没有更新。 The following code i tried but nothing worked out. 我尝试了以下代码,但没有解决。

 this.DropdownValues = ko.observableArray([
    { id: 0, type: "Arc",Checked:false },
    { id: 1, type: "Eve",Checked:false },
    { id: 2, type: "Ca",Checked:false },
    { id: 3, type: "test",Checked:false },
 ]);

Code I have written inside binding handler. 我在绑定处理程序中编写的代码。

 var value =  valueAccessor();
 var valueUnwrapped = ko.unwrap(value);
 console.log("true");
 valueUnwrapped.map(function(item){
     item[Checked]= true; return item; 
 });

 ko.utils.unwrapObservable(value(valueUnwrapped));

But my view still not detecting the values. 但是我的观点仍然没有发现这些价值。 foreach not refeshing in view. foreach不会刷新。

You were missing quotes around the word Checked . 您错过了Checked一词的引号。 It should be item['Checked'] or item.Checked rather than item[Checked] . 应该是item['Checked']item.Checked而不是item[Checked]

 ko.bindingHandlers.updateArray = { update: function (element, valueAccessor, allBindingsAccessor, viewModel) { var value = valueAccessor(); var valueUnwrapped = ko.unwrap(value); $("#before").text(JSON.stringify(valueUnwrapped)); console.log("true"); valueUnwrapped.map(function(item){ item['Checked']= true; return item; }); ko.utils.unwrapObservable(value(valueUnwrapped)); } } var viewModel = function(){ var self = this; self.DropdownValues = ko.observableArray([ { id: 0, type: "Arc",Checked:false }, { id: 1, type: "Eve",Checked:false }, { id: 2, type: "Ca",Checked:false }, { id: 3, type: "test",Checked:false }, ]); }; ko.applyBindings(new viewModel()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> Before:<br> <span id="before"></span> <div data-bind="updateArray: DropdownValues"> </div> <br><br> After:<br> <span data-bind="text: ko.toJSON(DropdownValues)"></span> 

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

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