简体   繁体   English

在rails中使用多选列表

[英]Using a multi select list in rails

I'm trying to make a multi select list box in Rails. 我正在尝试在Rails中创建一个多选列表框。 My view code is: 我的观看代码是:

<div>
  <%=nested_form_for(@allocation) do|builder|%>
    <%=builder.label :song_id, "Pick a song" %>

    <%=builder.select :song_id, options_for_select(
    Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ] }, 
    { include_blank: true, multiple: true, size: 5 }) %>

    <%=builder.submit "Add Song", class: "btn btn-large btn-primary" %>
  <% end %>
</div>

At the moment I just have the normal single selectbox but I want to convert this to a multiselect. 目前我只有正常的单选择框,但我想将其转换为多选。 Any pointers would be much appreciated. 任何指针都将非常感激。 Thanks in advance 提前致谢

This seems to have worked in my case: 这似乎适用于我的情况:

<%= builder.select(
    :song_id,
    options_for_select(@selections),
    {},
    {multiple: true, size: 10})
%>

Often you need to use a select_tag however there are lots of different ways this can work depending on where you are getting the data from 通常你需要使用select_tag,但是根据你从哪里获取数据,有很多不同的方法可以使用

<%= select_tag '@Mymodel[myattribute][]',
    options_from_collection_for_select(SelectionModel, "id", "title", @Mymodel.myattribute),
    :multiple => true, :size =>10 }
%>

perhaps yours would look something like 也许你的看起来像

<%= select_tag '@allocation[song_id][]',
    options_from_collection_for_select(Song.all., "id", "title", @allocation.song_id),
    { :multiple => true, :size =>10 }
%>

an example of this can be seen, here... 在这里可以看到一个例子......

http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/ http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/

If you want to do by jquery the following link will help you 如果你想通过jquery做,以下链接将帮助你

http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/

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

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