I have a huge json data source (over 50,000 + lines) loaded in memory from a static file.
Note: It’s not important why I have it in a static file.
I use select2 (v 4.0.5) that initializes as:
function initSelect2(selectName, dataSelect) {
var pageSize = 20;
$.fn.select2.amd.require(["select2/data/array", "select2/utils"],
function (ArrayData, Utils) {
function CustomData($element, options) {
CustomData.__super__.constructor.call(this, $element, options);
}
Utils.Extend(CustomData, ArrayData);
var initialValue = { "ID": 17003, "C": "200779", "N": "clanok baterie 2pzb150", "SC": 1 };
CustomData.prototype.query = function (params, callback) {
if (!("page" in params)) {
params.page = 1;
}
var data = {};
if (params.term != undefined && params.term != '') {
let subData = $.grep(dataSelect, function (n, i) {
if (n.text.indexOf(params.term) > -1) {
return n;
}
});
data.results = subData.slice((params.page - 1) * pageSize, params.page * pageSize);
}
else {
data.results = dataSelect.slice((params.page - 1) * pageSize, params.page * pageSize);
}
data.pagination = {};
data.pagination.more = params.page * pageSize < dataSelect.length;
callback(data);
};
$('#mySelect3').select2({
ajax: {},
dataAdapter: CustomData,
width: '100%'
});
});
}
I have one big problem. I can not set the value to select from jQuery. If I try like this:
$ ("#mySelect3").val(17003).trigger(“change”);
nothing will happen. But I think I’m doing it badly. If I understand the documentation I think I should implement function:
CustomData.prototype.current = function (callback) {}
that should create the data, and then function:
CustomData.prototype.query = function (params, callback) {}
should only filter them.
Can you please help me, how do I implement select2 initialization, that can work with many options and can by set from jQuery?