I'm trying to make it work
I have a rail form that looks like this:
%tr %td = f.label :company %td = f.hidden_field :companyid, class: 'select2 ajax', data: { source: companies_path }
inside my coffee js
$(document).ready -> $(".select2").each (i, e) -> select = $(e) options = {} if select.hasClass("ajax") options.ajax = url: select.data("source") dataType: "json" data: (term, page) -> q: term page: page per: 10 results: (data, page) -> results: data options.placeholder = "Select a value" options.dropdownAutoWidth = "true" select.select2 options return
The search works fine, the data storage also, but when I open the page again, that is, edit the saved record, I get an empty select2 object without the default value loaded in the select box.
It does not collect the existing value from the saved record and does not display it. I can search without problems - everything is in order, it just does not work.
Now I played with InitSelection and tried to set "val", but it just didn't work.
What is the correct way to load a value stored from a record into select2?
thanks
DECISION FROM EMAILENIN TO SEE BELOW
$(document).ready -> $(".select2").each (i, e) -> select = $(e) options = {} if select.hasClass("ajax") options.ajax = url: select.data("source") dataType: "json" data: (term, page) -> q: term page: page per: 10 results: (data, page) -> results: data options.placeholder = "Select a value" options.dropdownAutoWidth = "true" options.initSelection = (element, callback) -> data = {id: element.val().split('||')[0], text: element.val().split('||')[1]}; callback data select.select2 options return