jquery - select2 can i show the default list of options in the select2 ajax call -
how can show default list of options in select2 ajax call plugin.
before typing characters wanna show atleast 10 options in drop down list.
$(".doctor_id_pat1").select2({ placeholder: "search users", minimuminputlength: 0, ajax: { url: "test.php", datatype: 'json', data: function (term) { return { q: term }; }, results: function (data, page) { console.log(data); return { results: $.map(data, function (item) { return { text: item.text, id: item.id } }) }; } } });
select2 gives customizable select box support searching, tagging, remote data sets, infinite scrolling, , many other highly used options.
html
<input type="hidden" id="select" value="" style="width:300px;" /><br />
javascript
var default_options = [ { id: 'def1', text: 'default choice 1' }, //your options goes here ]; var ajax_options = [ { id: '1', text: 'choice 1' }, //your options goes here ]; var lastoptions = default_options; $('#select').select2({ minimuminputlength: 0, query: function(options) { if (options.term) { $.ajax({ type: 'post', url: '/echo/json/', datatype: 'json', data: { json: json.stringify(ajax_options), delay: 0.3 }, success: function(data) { lastoptions = data; options.callback({ results: data }); } }); } else { options.callback({ results: lastoptions }); } } });
fiddled here
Comments
Post a Comment