Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect2 in a complex form as abstract class
    text
    copied!<p>I need to use the select2 object in my form. This is my form <img src="https://i.stack.imgur.com/jVILq.jpg" alt="enter applicant form"> <a href="https://i.stack.imgur.com/jVILq.jpg" rel="nofollow noreferrer">http://i.stack.imgur.com/jVILq.jpg</a></p> <p>There are many select html objects.</p> <p>For instance If I would like to change the Customers select box into a Select2 object I have written this little snipped of code posted at jsfiddle.net but I cannot create a copy of this function for each select because it too difficult to maintain.</p> <p>How have I to abstract it?</p> <p>I have posted a sample here: <a href="http://jsfiddle.net/GcJgU/7/" rel="nofollow noreferrer">http://jsfiddle.net/GcJgU/7/</a></p> <p>I have already found a potential solution from the user Flip but it is not complete because I need to apply this JQuery object to all the input hidden html objects in the page.</p> <p>This is an example:</p> <pre><code>$(".select2").select2({ ajax: { url: $(this).attr("url-search") + "/term/", dataType: 'json', cache: true, data: function (term, page) { return { term: term }; }, results: function (data) { var results = []; $.each(data, function (index, item) { var id = $(this).attr("field-id"); var fieldname = $(this).attr("fields-data"); results.push({ id: item[id], text: item[fieldname] }); }); return { results: results }; }, }, formatResult: function (object, container, query) { console.log(object); }, initSelection: function (element, callback) { var id = $(element).val(); var fieldid = $(element).attr("field-id"); var fieldname = $(element).attr("fields-data"); $.ajax($(element).attr("url-searchid") + "/term/" + id, { dataType: "json" }).done(function (items) { var data = { id: items[0][fieldid], text: items[0][fieldname] }; callback(data); }); } }); </code></pre> <p>Seems that $(this).attr("url-search") is not read and the search process doesn't start. I don't understand why.</p> <p>Thanks guys</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload