Select2 Ajax calls ,sorting,filtering data server side in jquery
var global_data_Store=null;
$(".selectpicker").select2({
placeholder: ' Select a Parent Group ',
//minimumInputLength: 2,
language: "en",
ajax: {
url: "https://dev1.owlapplicationbuilder.com/appsbuilder/objectsbuilder/api/v1/workflow/metaNew",
type: "GET",
dataType: 'json',
delay: 250,
allowClear: true,
matcher: function(term, text, option) {
debugger
return text.toUpperCase().indexOf(term.toUpperCase())>=0 || option.val().toUpperCase().indexOf(term.toUpperCase())>=0;
},
data: function (params) {
return {
searchTerm: params.term// search term
};
},
processResults: function (response) {
debugger
var obj=response.apiMetaInfo;
global_data_Store=response.apiMetaInfo;
var search=$(".select2-search__field").val();
if(search!=="" && search!==null && search!==undefined){
debugger;
var result_filter=null
result_filter=global_data_Store.filter(function(item) {
return item.name.toLowerCase().indexOf(search) > -1
});
debugger;
return {
results: $.map(result_filter, function (item) {
return {
text: item.name,
id: item._id
}
})
};
}
else{
return {
results: $.map(obj, function (item) {
return {
text: item.name,
id: item._id
}
})
};
}
},
cache: true
},
dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
escapeMarkup: function (m) {
return m;
},
sorter: function(data) {
return data.sort(function(a, b) {
return a.text < b.text ? -1 : a.text > b.text ? 1 : 0;
});
},
});