[英]Adding Chosen to a Rails App
I have been looking around and cannot find a good example of implementing Chosen, http://harvesthq.github.com/chosen/ , into a Rails app. 我一直在环顾四周,无法找到一个很好的例子来实现Chosen, http://harvesthq.github.com/chosen/ ,进入Rails应用程序。 I am trying to convert my existing multi-select into a chosen styled multi-select box. 我正在尝试将现有的多选项转换为选定的样式多选框。
I tried inserting the files manually, copying chosen.jquery.js into my assets/javascripts folder along with adding chosen.css. 我尝试手动插入文件,将chosen.jquery.js复制到我的assets / javascripts文件夹中,同时添加chosen.css。 Also added //= require chosen-jquery to application.js along with the following code 还添加了// = require selected-jquery到application.js以及以下代码
jQuery(function($){
$('.chzn-select').chosen();
})
Also have *= require chosen in application.css.scss 也可以在application.css.scss中选择* = require
I added :class => "chzn-select" in a collection_select in a form_tag. 我在form_tag的collection_select中添加了:class =>“chzn-select”。
Also tried using the 'chosen-rails' gem (here is an example http://choppingbloc.tumblr.com/post/24894460392/multiple-select-boxes-with-chosen-jquery ) but had no luck. 还尝试使用'selected-rails'宝石(这里有一个例子http://choppingbloc.tumblr.com/post/24894460392/multiple-select-boxes-with-chosen-jquery ),但没有运气。
Anything I might be overlooking? 我可能会忽视什么? The collection_select is working, but it is not styled like Chosen. collection_select正在运行,但它没有像Chosen那样的样式。 I am a beginner so if there is anything obvious I may have overlooked, please tell - everything above is what I have done. 我是初学者,所以如果有任何明显的东西我可能会被忽视,请告诉我 - 以上所有内容都是我所做的。
Set it up like this 像这样设置它
You have to include both the css file and the js file 您必须包含css文件和js文件
in you application.css.scss, (because it is sass, do not use /* require) 在您的application.css.scss中,(因为它是sass,不要使用/ * require)
@import "chosen";
in you application.js 在你的application.js
//= require chosen.jquery
You should give your selects a different class or id than 您应该为您的选择提供不同的类或ID
$('.chzn-select').chosen();
because, it might make things messy when you debug the html. 因为,调试html时可能会让事情变得混乱。 Chosen gives classes with this prefix like .chzn-done, .chzn-single, chzn-drop, chzn-search. Chosen给出了带有这个前缀的类,如.chzn-done,.chzn-single,chzn-drop,chzn-search。
If you have a railscasts pro account. 如果您有railscasts pro帐户。
look at this 看这个
You should really drop the chosen plugin and go for the select2 plugin. 您应该放弃所选的插件并转到select2插件。 It is based on the chosen plugin, but is way better. 它基于所选的插件,但更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.