6

I have a problem when binding a list of complex values (id/ description) to a list of checkboxes.

The problem is that I need not just the id or the description, but both.

When I bind using the Checked binding, all the checkboxes in my list are checked when I click one item.

This is part of something bigger, and the reason I want the object and not just the id is that the complex type in turn will have a list which I want to bind to another list of checkboxes.

Here is a fiddle that produces the problem: http://jsfiddle.net/M8KFd/1

Thankful for your help.

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79

2 Answers2

10

I track the state of each item individually, then get the selected items using a dependent observable something like this

Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • Awesomeness! My list is serialized data from the backend, but I ended up looping the initial array, slapping on the selected property: http://jsfiddle.net/M8KFd/2/ BTW, You should consider using `ko.utils.arrayFilter` instead of `$.grep` to avoid mixing dependencies in the code. Thanks! – Mikael Östberg Nov 18 '11 at 10:41
0

A simpler solution, is to fake the checkbox with css, then with knockout just call a method which will pass the whole item to the viewmodel:

  <li data-bind="click: $parent.setCompany">
        <span data-bind="text: CompanyName"></span>,
        <span data-bind="text: RegAddress_PostTown"></span>
   </li>

Of course this does not work for all scenarios as per Richard Friend's answer.

rjarmstrong
  • 1,221
  • 12
  • 22