Using the checked binding in knockout with a list of checkboxes checks all the checkboxes

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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.

回答1:

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



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!