【发布时间】:2015-09-29 09:33:46
【问题描述】:
在 rails 4.2.2 中,我使用 chosen jquery 插件作为 modal 表单内的下拉(多个)字段。现在表单和字段正在加载,没有错误,但集合(列表)没有加载,如果我尝试搜索,它会显示“没有结果...”。
在控制器中,
@users = User.all.collect(&:name)
在视图中,
<%= f.input :users, :as =>:select, :label=>false, :collection =>[], :input_html=>{:multiple=>true, :class=>"form-control"} %>
在 view.js.erb 中
var content = "<%= escape_javascript(render(:partial=>"form")) %>";
var container = $('#main-lightbox-container');
container.find('.modal-content').html(content);
container.modal({});
initializeMultipleChoose("#event_users", JSON.parse('<%= raw(@users) %>'), {width: "95%", max_selected_options: 5, allow_single_deselect: true});
$("#event_users").trigger("chosen:updated");
在自定义 .js 文件中,
function initializeMultipleChoose(id, selectedOptions, parameters){
selectedOptionsArray = $.isArray(selectedOptions) ? selectedOptions : [selectedOptions]
object = $(id);
$.each(object.find("option"), function(key, content){
option = object.find(content);
if(selectedOptionsArray.indexOf(option.val())> -1)
option.attr('selected',true);
})
object.chosen(parameters);
}
加载列表需要做什么?请帮帮我。
【问题讨论】:
标签: javascript jquery ruby-on-rails jquery-chosen