使用knockoutJS,如何创建多选列表
我试图在使用 KnockoutJS 时拥有 jQuery 可选择列表 的功能,但无法获取它去工作。
使用自定义绑定,我成功地将 ul
元素转换为可选择的元素,并为选定和未选定的事件创建事件处理程序。但是我应该将什么传递给维护所选元素的 ViewModel 函数呢?
到目前为止,这是我的代码: http://jsfiddle.net/QCmJt/
我对这一切都很陌生,所以也许我正在以错误的方式处理这个问题......
I am trying to have the functionality of the jQuery selectable list when using KnockoutJS, but can't get it to work.
Using Custom Bindings I was successful in transforming ul
element in a selectable one and creating events handlers for the selected and unselected events. But what do I pass to my ViewModel functions which maintain the selected elements?
Here is my code so far: http://jsfiddle.net/QCmJt/
I am new to all this, so maybe I am approaching this the wrong way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您的一点修改,也可以正常工作。它使用数组中每个可观察项的选定属性来进行双向通信。如果您想要一个只是所选项目列表的属性,您始终可以创建一个依赖的可观察量,或者只是一个过滤方法。
http://jsfiddle.net/QCmJt/32/
创建了自定义绑定:
它没有针对更多内容进行强化比非常简单的双向绑定更简单,但应该足够容易构建。
Here is a little bit of a modified one from yours, got it working as well. It uses a selected property on each item in the array that's an observable to do two way communication. If you want a property that's just a list of the selected items, you could always create a dependent observable, or just a method to filter.
http://jsfiddle.net/QCmJt/32/
Created a custom binding:
It's not hardened for more than very simple two way bindings, but should be easy enough to build on.
好吧,我想出了一种方法来传递绑定到所选项目的数据:
$(ui.selected).tmplItem().data
当选择和$(ui.selected).tmplItem().data
取消选择时。这里是更新的小提琴: http://jsfiddle.net/8RnxC/
自定义绑定如下所示:
我的视图模型:
Ok, I figured a way to pass the data bound to the selected item:
$(ui.selected).tmplItem().data
when selecting and$(ui.selected).tmplItem().data
when unselecting.Here the updated fiddle: http://jsfiddle.net/8RnxC/
Custom binding looks like:
And my view model: