简体   繁体   中英

select2 gem remember selected options after search

I have used select2 gem in my rails 6 project to show multiple options to select

<%= select_tag 'skill[]', options_for_select(Course.populate_data, :selected=> "#{params[:skill] rescue nil}"), :prompt => "skill", class: "form-control js-select",id: "js-skill", :style => "width:180px", multiple: "multiple", :style => "width:280px;"%>&nbsp;&nbsp;

I want selectbox to remember my last selections after I submitted the search form

I tried data amd params, but not working for me, can anyone help me with this

I solved the issue, using below code. I was not doing AJAX call, otherwise I would have done this in success response of query

$(".js-select").select2();
if ("<%=params[:skill]%>") {
  var a = <%=raw params[:skill]%>
  $('#js-skill').select2().val(a).trigger('change');
} else {
  $("#js-skill").select2();
}

Posting the answer, if it may help someone

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