简体   繁体   中英

how to avoid prompt as selected in select2 gem

I have used select2-rails gem. When it is applied, the prompt is also selectable. How to avoid prompt as selected?

You can also do something like this on the element without having to specify placeholder in Javascript.

<%= f.collection_select :method, Model.all, :id, :name,
{include_blank: true}, data: {placeholder: 'Choose Model'}, class:
"select2"%>

assuming you have initialized this element with select2 somewhere in your javascript files.

使用'placeholder'选项,如下所示:

$(element).select2(placeholder: 'select one option', ** other options here);

IN YOURVIEW.HTML.SLIM

select.form-control#js__multiselect multiple="multiple" name='your_name[]' value="#{params[:your_value]}"
  = options_from_collection_for_select(Model.all, :id, :value, params[:your_params])

# :id, :value are what values you want to show from your Model.
# params[:your_params], which is array is your selected value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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