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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to update the code, else search stopped working