Last active
August 29, 2015 14:17
-
-
Save paveltyavin/c9f4e304cb5ad1e61372 to your computer and use it in GitHub Desktop.
Select2 paginate without ajax using query method. You can fetch data from server first or from localstorage and then paginate it in select2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
city_list = [{name: 'Moscow', id: 1},{name: 'Saint Petersburg', id: 2}] | |
$('.city').select2 | |
query: (options) -> | |
term = options.term.toLowerCase() | |
results = [] | |
output = {} | |
paginate_by = 50 | |
i = 0 | |
j = 0 | |
while results.length < paginate_by and i < city_list.length | |
i += 1 | |
city = city_list[i - 1] | |
if city.name.toLowerCase().indexOf(term) > -1 | |
j += 1 | |
if j > (options.page - 1) * paginate_by | |
results.push(id: city.id, text: city.name) | |
output.results = results | |
output.more = i < city_list.length | |
options.callback(output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
city_list = [{name: 'Moscow', id: 1},{name: 'Saint Petersburg', id: 2}] | |
$('.city').select2({ | |
query: function(options) { | |
var city, i, j, output, paginate_by, results, term; | |
term = options.term.toLowerCase(); | |
results = []; | |
output = {}; | |
paginate_by = 50; | |
i = 0; | |
j = 0; | |
while (results.length < paginate_by && i < city_list.length) { | |
i += 1; | |
city = city_list[i - 1]; | |
if (city.name.toLowerCase().indexOf(term) > -1) { | |
j += 1; | |
if (j > (options.page - 1) * paginate_by) { | |
results.push({ | |
id: city.id, | |
text: city.name | |
}); | |
} | |
} | |
} | |
output.results = results; | |
output.more = i < city_list.length; | |
return options.callback(output); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment