简体   繁体   English

Rails,集合选择样式

[英]Rails, collection select style

I am trying to style a collection select without any luck... Currently, I am picking a meal_size like this: 我正在尝试设置没有任何运气的精选收藏集的样式。目前,我正在选择以下餐点尺寸:

= f.input :meal_size_id, collection: @meal.meal_sizes

Which generates 哪个产生

<div class="form-group select optional order_meal_size_id">
<select class="select optional form-control" placeholder="Meal size" name="order[meal_size_id]" id="order_meal_size_id">
   <option value=""></option>
   <option value="10">Small</option>
   <option value="11">Medium</option>
   <option value="12">Large</option>
</select>
</div>

However, what I want is to have a whole div inside the option tag, and not just some plain text. 但是,我想要的是在option标记内有一个整个div,而不仅仅是一些纯文本。 Also, I would like the options to be listed, rather than shown in a dropdown. 另外,我希望列出这些选项,而不是显示在下拉列表中。 Any ideas on how to accomplish that :? 关于如何实现这一目标的任何想法:? Thank you! 谢谢!

Edit: Previously, my meal_sizes were picked using jquery. 编辑:以前,我的饭食大小是使用jquery选择的。 The divs were generated like this: div是这样生成的:

= div_for(meal_size, class: "col-xs-4")
  .bold
    = meal_size.name
  .prices
    = meal_size.price
  .short_description
    = meal_size.description

And I had this code to put the selected ones in a hidden field 我有这段代码将选定的代码放在隐藏字段中

  $('.meal_size').click ->
    html_id = $(this).attr('id')
    meal_size_id = html_id.split('_')[2]
    $("#order_meal_size_id").val(meal_size_id);
    return

I would like to have the same as an option so that when picking them I am not using jquery. 我希望具有与选项相同的功能,以便在选择它们时不使用jquery。

<div class="form-group">
  <%= f.label :meal_sizes, class: "col-md-4 control-label" %>
  <div class="col-md-4">
    <%= collection_select(:meal_sizes, @meal.meal_sizes, :id, :name, {:prompt => false}, class: "form-control") %>
  </div>
</div>

You should Use This:- Make your method(meal_sizes) as return like and pass it in the collection 您应该使用以下方法:-使您的方法(meal_sizes)像返回一样,并将其传递给集合

arr = ["<div>Small</div>","<div>Medium</div>","<div>Large</div>"]

f.input :meal_size_id, collection: arr

...what I want is to have a whole div inside the option tag ...我想要的是在选项标签中包含整个div

As far as I know, it is invalid to have any other html tags inside of an option tag. 据我所知,在选项标签内包含任何其他html标签是无效的。

Also, I would like the options to be listed, rather than shown in a dropdown. 另外,我希望列出这些选项,而不是显示在下拉列表中。 Any ideas on how to accomplish that 关于如何做到这一点的任何想法

Select tag in html has a size attribute, eg which shows the first n options in the select tag. html中的选择标签具有大小属性​​,例如,它显示选择标签中的前n个选项。 You could write some code in rails to make the size attribute equal to the count of the collection you are displaying in the drop down list. 您可以在rails中编写一些代码,以使size属性等于您在下拉列表中显示的集合的数量。

For example, you could add { size: @meal.meal_sizes.count } to the html options hash of the select tag. 例如,您可以将{ size: @meal.meal_sizes.count }添加到select标签的html options哈希中。

<%= f.collection_select(:meal_size_id, @meal.meal_sizes, :id, :name, {:prompt => false}, { class: "form-control", size: @meal.meal_sizes.count }) %>

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

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