Skip to content

Instantly share code, notes, and snippets.

@puppe1990
Last active September 6, 2018 16:18
Show Gist options
  • Save puppe1990/ea159c55c9dc554b32e1e234336b1b47 to your computer and use it in GitHub Desktop.
Save puppe1990/ea159c55c9dc554b32e1e234336b1b47 to your computer and use it in GitHub Desktop.
example to select2 when json return in bigger
$("#ajax-example").select2({
ajax: {
url: 'yourReturn.json',
dataType: 'json',
delay: 350,
data: function(params) {
return {
q: params.term // search term
};
},
processResults: function(data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
var resData = [];
data.forEach(function(value) {
if (value.full_name.indexOf(params.term) != -1)
resData.push(value)
})
return {
results: $.map(resData, function(item) {
return {
text: item.full_name,
id: item.id
}
})
};
},
cache: true
},
minimumInputLength: 3
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment