Last active
October 16, 2019 05:34
-
-
Save M-Yankov/1451da29eddbd1bc71c0975af80e63ae to your computer and use it in GitHub Desktop.
select2 dropdown: When loading results with ajax, load them once. (the results are always the same)
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
var s2data = $allUnitsSelect.select2({ | |
ajax: { | |
url: '/GetValues', | |
dataType: 'json', | |
data: function (params) { | |
var query = { | |
text: text, | |
} | |
return query; | |
}, | |
processResults: function (responseValues) { | |
var listItems = mapResponseData(responseValues); | |
this.options.set('cacheDataSource', listItems); | |
return { results: listItems }; | |
} | |
}, | |
cacheDataSource: [], | |
allowClear: true, | |
placeholder: 'Select Values ...', | |
width: '100%', | |
}).data('select2'); | |
s2data.dataAdapter.query = function (params, callback) { | |
var cacheDataSource = this.options.get('cacheDataSource'); | |
if (cacheDataSource && cacheDataSource.length > 0) { | |
callback({ results: cacheDataSource }); | |
} | |
else { | |
// call the original logic | |
var ajaxAdapterFunc = jQuery.fn.select2.amd.require('select2/data/ajax'); | |
var ajaxAdapter = new ajaxAdapterFunc(this.$element, this.options); | |
ajaxAdapter.query(params, callback); | |
} | |
} |
I had to update the code, else search stopped working
s2data.dataAdapter.query = function (params, callback) {
var cacheDataSource = this.options.get('cacheDataSource');
if (cacheDataSource && cacheDataSource.length > 0) {
callback(
{
results: cacheDataSource.filter(function (b) {return b.text && b.text.toLowerCase().includes(params.term.toLowerCase()); })
});
}
else {
var ajaxAdapterFunc = jQuery.fn.select2.amd.require('select2/data/ajax');
var ajaxAdapter = new ajaxAdapterFunc(this.$element, this.options);
ajaxAdapter.query(params, callback);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do not know exactly your case (context) The will be the same every time or the data needs to be fetched each type when the user focuses out of the search field. The example above is complex for specified version of select and it will load the results with ajax only the first time. If the page is reloaded the select2 dropdown needs to make another ajax to load the data (It was very specific in my case).
If you are not using any library/framework like angular, backbone, react, vue.js etc. then
localStorage
,sessionStorage
andcookies
are available as option to store data.Otherwise you can use Server side to render the data along with HTML. And then when initialize select2 use something like:
Or you can load the data from storage when the page loads:
But the property
cacheDataSource
in my example always starts with empty array and it is per dropdown.In short, you cannot use that property when you want to initialize select2's data.