jQuery Chosen - 更新选择列表而不丢失选择
我正在尝试使用 jQuery 插件“Chosen”
(http://harvesthq.github.com/chosen/ 和 https://github.com/harvesthq/chosen)
在我的项目中。
我想要实现的是根据用户选择更新列表(ajax
调用(基于树的结构
))
这不是更大的问题,因为我可以使用 .chosen().change(function())
并删除所有未使用的选择项,然后 .append 新的。
然后我可以使用 .trigger("liszt:updated")
来更新列表,但不幸的是所有选择都被删除。
有谁知道如何在不丢失所选数据的情况下更新所选列表的方法?
理论上,我可以手动删除所有选定的生成
I'm trying to use jQuery plugin "Chosen"
(http://harvesthq.github.com/chosen/ and https://github.com/harvesthq/chosen)
in my project.
What I'm trying to achieve is update list basing on user selection (ajax
call (tree based structure
))
This is no bigger problem, because i can use .chosen().change(function())
and remove all unused select items and then .append new ones.
Then I can use .trigger("liszt:updated")
to update list, but unfortunately all selections are deleted..
Does anyone know a way how to update chosen list without loosing selected data?
In theory I can manually remove all chosen generated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您保存所选项目,这应该相当简单。例如:
现在,在更新所选项目之前,请确保像这样保存所选项目:
这应该能够重置更改之前的原始值。
This should be fairly simply if you save the items selected. For example:
Now, before updating the chosen, make sure you save the items selected like this:
This should be able to reset the original values before the change.
新代码现在更新列表而不会丢失选择,并且根据选项顺序对选择进行排序。
参考他们的项目页面。
The new code now updates the list without losing the selections, and it sorts the selections based on the options order.
Reference their project page.
这将在 xhr 请求(刷新列表)后重新加载选择,并在新项目列表不包含先前选择的项目时删除选择:
This will reload the selection after xhr request (refresh list) and delete the selection if the new item list not contains the earlier selected item:
我使用 selected 创建了一些级联或依赖下拉菜单,但除了 knockoutjs 之外我还使用了它们。 KnockoutJS 用于将数据(在您的例子中是选择)绑定到对象和 DOM 元素。 Knockout 还允许您创建自定义绑定来处理他们可能没有预料到的开箱即用的事情。话虽这么说,我为淘汰赛创建了一个使用 Chosen 的自定义绑定,结果很好...
在我们的例子中,我们允许用户选择一个频道(使用 selected),然后我们加载到他们的位置(通过显示或创建另一个选择)元素)并触发我们的自定义绑定,该绑定将更新数据并触发我们的自定义绑定,该绑定将告诉所选对象运行
.trigger("liszt:updated")
但将数据保留在后台。我们的代码是相当专有的,我不知道它是否一定会轻松地向您展示如何实现这一目标,但这也许会给您提供另一种看待它的方式。
I have created a few cascading or dependent dropdowns using chosen, but I have used them in addition to knockoutjs. KnockoutJS is used for binding data (in your case the select) to an object and a DOM element. Knockout also allows you to create custom bindings to handle things they may not have anticipated straight out of the box. With that being said I created a custom binding for knockout that utilized Chosen and it turned out well...
In our case we allow users to select a channel (using chosen) we then load in their locations (either by displaying or creating another select element) and trigger our custom binding which will update the data and trigger our custom binding that will tell chosen to run
.trigger("liszt:updated")
but keep the data in the background.Our code is rather proprietary and I don't know that it would necessarily show you easily how to achieve this, but perhaps this will give you another way of looking at it.