I am trying to load data for jQuery plugin Selectize.js via ajax, but I am not able to delete options loaded with previous calls from dropdown element.
I am stuck with this code where I am able to load data via ajax and show them in dropdown, but I would like to delete them when dropdown is closed (because also typed text is deleted) and load a new ones from another ajax call (after new text is typed), but commands clearOptions(true) / refreshItems()
does not delete anything. I would like to delete only items in dropdown. If any item is selected, it should stay so.
var selectize = $('#selectize').selectize({
sortField: 'text',
loadThrottle: 500,
load: function(query, callback) {
if (query.length < 2) return;
selectize[0].selectize.clearOptions(true); // this line does not do anything with options in dropdown menu. Even in combine with the line below.
selectize[0].selectize.refreshItems();
// simulation of ajax load
var ajax_data = [{'value': '31', 'text': 'aaa yyy'}, {'value': '32', 'text': 'aaa xxx'}, {'value': '33', 'text': 'bbb xxx'}];
callback(ajax_data);
selectize[0].selectize.open();
}
});