Using the checked binding in knockout with a list

2019-04-29 03:19发布

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.

标签: knockout.js
2条回答
三岁会撩人
2楼-- · 2019-04-29 03:30

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-29 03:49

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.

查看更多
登录 后发表回答