Raimonds
Raimonds

Reputation: 537

knockout checked binding with dynamicly created checkbox list

my scenario is as follows: I have 3 buttons, on click I am calling function which loads array of objects. This array is used to create table for each object in it. Table row contains UserName, UserType and checkbox bound to coresponding object. binding goes as follows

data-bind="checkedValue: $data, checked: $root.selectedRecipients"

$data contains right values, which are passed to selectedRecipients observableArray as objects.

My issue is that whenever I press button, table is re-created with new checkboxes, but they ain't checked if selectedRecipients still containt the same values. If I check checkbox, dublicated value is added to array.

Question: What should I do to make those new checkboxed pick up data from observable array? What are my options

Update: My best guess is that when server pulls new data with object, they are considered different. So should I create custombinding for ko's checked to make it comparare things based on object values not object itself?

Upvotes: 0

Views: 509

Answers (1)

Fabio
Fabio

Reputation: 11990

You can do the following:

 <-- ko foreach: arrayOfObjects -->
    <input type="checkbox" data-bind="value: recipientId, checked: $root.selectedRecipients">
 <-- /ko -->

Now selectedRecipients holds only the "id". You can recreate the array of objects as you want, the selected object will remain selected.

More information at http://knockoutjs.com/documentation/checked-binding.html

Upvotes: 0

Related Questions