簡體   English   中英

在rails中使用多選列表

[英]Using a multi select list in rails

我正在嘗試在Rails中創建一個多選列表框。 我的觀看代碼是:

<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>

目前我只有正常的單選擇框,但我想將其轉換為多選。 任何指針都將非常感激。 提前致謝

這似乎適用於我的情況:

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

通常你需要使用select_tag,但是根據你從哪里獲取數據,有很多不同的方法可以使用

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

也許你的看起來像

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

在這里可以看到一個例子......

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

如果你想通過jquery做,以下鏈接將幫助你

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM