代码之家  ›  专栏  ›  技术社区  ›  Zulhilmi Zainudin

如何在Rails表单选择标记中添加数据属性?

  •  23
  • Zulhilmi Zainudin  · 技术社区  · 9 年前

    我找到了这个 Bootstrap Select 项目及其 gem for Rails 。我想在select标记中实现搜索。

    我确实检查了元素,这里是HTML源代码:

    <select class="selectpicker" data-live-search="true">
      <option>Hot Dog, Fries and a Soda</option>
      <option>Burger, Shake and a Smile</option>
      <option>Sugar, Spice and all things nice</option>
    </select>
    

    如何添加 data-live-search="true" 在我的表单选择标签中?

    我的表单选择:

    <%= f.select :food_id, options_for_select(Food.all.map{|c| [c.name, c.id]}, f.object.food_id), {}, {class: "form-control selectpicker"} %>
    

    我尝试过的:

    <%= f.select :food_id, options_for_select(Food.all.map{|c| [c.name, c.id]}, f.object.food_id), {}, {class: "form-control selectpicker", data: "live-search"} %>
    

    但它不起作用。

    4 回复  |  直到 9 年前
        1
  •  31
  •   Roman Kiselenko    9 年前

    尝试:

    {class: "form-control selectpicker", "data-live-search" => "true" }
    
        2
  •  22
  •   un_gars_la_cour    9 年前
    <%= f.select :food_id, options_for_select(Food.all.map{|c| [c.name, c.id]}, f.object.food_id), {}, {class: "form-control selectpicker", data: {"live-search": true}} %>
    
        3
  •  3
  •   gilcierweb    8 年前
      <%= f.select(:plan_id, {},{}, {'v-model'=>"plan_id",'@change'=>"onChange", class: "form-control"}) do  %>
    <% Plan.all.each do |plan| %>
        <%= content_tag(:option,"#{plan.name.upcase} #{plan.max_items}  #{number_to_currency(plan.price)}", value:plan.id, :data => {items: plan.max_items, price: plan.price}) %>
    <% end%>
    <% end%>
    
        4
  •  0
  •   Dorian    4 年前

    如果要将块传递给select,则需要传递 {} 两次,例如。

        => f.select :to_user_id, {}, {}, data: { placeholder: "Choisis des invités..." }, class: "chosen-image" do
          - User.find_each do |user|
            option(data-img-src="#{user_image_url_for(user)}" value="#{user.id}")
              = user.first_name