Skip to content

Instantly share code, notes, and snippets.

@alexweissman
Created August 9, 2017 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexweissman/343ecc06946a8fdd79a887c7d3a5760f to your computer and use it in GitHub Desktop.
Save alexweissman/343ecc06946a8fdd79a887c7d3a5760f to your computer and use it in GitHub Desktop.
Example setup for select2 with pagination and a Sprunjed endpoint
// Institution select
form.find("select[name='institutions[]']").select2({
ajax: {
url: site.uri.public + "/api/institutions",
dataType: "json",
delay: 250,
data: function (params) {
return {
filters: {
name : params.term
},
page: params.page || 0,
size: params.size || 10
};
},
processResults: function (data, params) {
var suggestions = [];
// Process the data into dropdown options
if (data && data.rows) {
jQuery.each(data.rows, function(idx, row) {
row.text = row.slug;
suggestions.push(row);
});
}
params.page = params.page || 0;
params.size = params.size || 10;
return {
results: suggestions,
pagination: {
more: (params.page * params.size) < data.count_filtered
}
};
}
},
width: '100%',
templateResult: function(item) {
// Display institution name as tag option
return $("<div>" + item.name + "</div>");
},
cache: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment