javascript - Knockout checkedValue binding not working when array is made up of objects -


With a page on my site, I'm having a strange problem. I send model data to the page on ASP.NET I am using MVC.

I am showing a list of checkboxes based on the objects in a JavaScript array. When the page loads, it displays the item correctly, but does not select those people in the selected items.

Looks like HTML here

  & lt ;! - - Aggram: Item - & gt; & Lt; P & gt; & Lt; Input type = "checkbox" data-bind = "check value: $ data, checked: $ root.selectedItems" /> & Lt; Span data-bind = "text: name" & gt; & Lt; / Span & gt; & Lt; / P & gt; & Lt ;! - / ko - & gt;   

Looks like javascript here

  var item = function (id, name) {this.Id = id; This.Name = name; }; Var model = {items = ko.observableArray ([]), selected items = co.obsbian array ([])}; @forcha (server in server server) {@: model.items.push (new item ('@ serverItem.Id', '@ serverItem.Name')); } @Worch (Selected Search ITEM in Selected Surveyor Items) {@: model.selectedItems.push (new item ('@ selectedServerItem.Id', '@ selectedServerItem.Name')); } Ko.applyBindings (model);   

You would think that it will be selected because things are the same, but it is not. And when I select the checkboxes, it adds additional code instead of the use of existing items. ['1', 'Business to Business'], ['2', 'Business to Consumer'], ['1', 'Business to Business']

Can anyone explain to me why this is happening? Why is the knockout not understood that checkboxes should be examined on the basis of objects?

Finished with the solution:

  var items = []; @forcha (server server in server server) {@: item [serverItem.Id] = {id: '@ serverItem.Id', name: 'serverItem.Name'}; @: Model.items.push (item [@ serverItem.Id]); If (selected server) any (C => si.Id == serverItems.Id) @: model.selectedItems.push (item [@ serverItem.Id]);}   

Now when the page loads, the checkboxes are selected properly.

Objects are identical, but these are different examples. To compare objects, a == (or maybe === ) does, and in javascript which is always for 2 different instances of an object Lies will return, even if they are the same internally. It works fine for strings, though.

  var foo1 = {foo: 1}; Var foo2 = {foo: 1}; Var foo3 = foo1; Console.log (foo1 == foo2); // false console.log (foo1 == foo3); // True   

You can see in action in this Bella ...

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -