简体   繁体   English

与dropoutchecklist一起使用dropdownchecklist

[英]getting dropdownchecklist working with knockoutJS

I'm trying to get dropdownchecklist jquery plugin working with ko. 我试图让dropdownchecklist jQuery插件与ko一起工作。 I've wired up custom binding handler but dropdown won't populate with options. 我已经连接了自定义绑定处理程序,但是下拉列表中不会包含选项。 Please check my fiddle: http://jsfiddle.net/amitava82/wMH8J/11/ 请检查我的小提琴: http : //jsfiddle.net/amitava82/wMH8J/11/

Appreciate your help. 感谢您的帮助。 Thanks! 谢谢!

This is because you create dropdown before binding KnockoutJS. 这是因为您绑定KnockoutJS 之前创建了下拉列表。 How does this dropdown work? 这个下拉菜单如何运作? It creates additional divs and spans which copy the content of select and create nice looking list. 它创建了其他divspan ,它们复制select的内容并创建漂亮的列表。 After that bindings are applied and they modify the select (as they should), but dropdown is not updated, because this library is kind of static, ie it copies the content of select only at the time of calling. 在应用绑定之后,绑定将修改select(如应有的那样),但是下拉列表不会更新,因为该库是静态的,即它仅在调用时才复制select的内容。

I've updated your jsFiddle so you can see temporary fix. 我已经更新了您的jsFiddle,以便您可以看到临时修复程序。 What I mean is that it works now, the binding is applied before creating dropdown. 我的意思是,现在可以正常工作, 创建下拉列表之前先应用绑定。 The only problem is that changing options field in viewModel won't affect the dropdown. 唯一的问题是,更改viewModel options字段不会影响下拉列表。 What you probably need to do is to use subscribe method . 您可能需要做的是使用subscription方法 You have to monitor changes to options field and if they occur you have to recreate the dropdown. 您必须监视对options字段的更改,如果更改发生,则必须重新创建下拉列表。 That's an easy way at least. 至少这是一种简单的方法。

@freakish answer will work for most static content, but for anything dynamic using templates, for example if or foreach bindings, or you need to support underlying data updates, such as more checkbox options "suddenly" becoming available, it will not work. @freakish答案适用于大多数静态内容,但适用于使用模板的任何动态内容,例如ifforeach绑定,或者您需要支持基础数据更新,例如更多“突然”可用的复选框选项,将不起作用。

An example of a really simple $.button binding apply which can be used to wrap the more simple jQuery calls. 一个非常简单的$.button绑定 apply示例,可用于包装更简单的jQuery调用。 It's a simple matter of adding more members to controls to make them available in bindings. controls添加更多成员以使其在绑定中可用,这很简单。

The case with jQuery Dropdown Check List is a bit tricky however, since you obviously want to use the built in options handler, but you need to run $.dropdownchecklist after the options handler has run, as it creates the DOM elements that jQuery depends on. 使用jQuery下拉检查单的情况是有点棘手,但是,因为你明明想使用内置的options处理程序,但你需要运行$.dropdownchecklistoptions处理程序运行,因为它创建的DOM元素的jQuery取决于。 By wrapping the built in options handler , we are always called in the correct context. 通过包装内置的options处理程序 ,我们总是在正确的上下文中被调用。

In my experience of usage (our project makes use of about 10-15 custom bindings), you'll average about 10-20 lines of actual JS. 以我的使用经验(我们的项目使用约10-15个自定义绑定),您平均会得到约10-20行的实际JS。 If you start ballooning into +100 lines, I find it's a good idea to refactor, and rethink. 如果您开始膨胀到+100行,我认为重构和重新思考是个好主意。 I hope this helps some :-) I've been using Knockout for a few months now at a major UI implementation project at work, I've really learned alot, and I'm amazed at this stuff. 我希望这对某些人有帮助:-)我在工作的一个主要UI实施项目中使用Knockout已有几个月了,我真的学到了很多,对此感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM