I'm using Select2 v4. I have an object and I like to get all the data of that object. When I select and option and use $(_id).select2('data')[0]), it returns all the data of the selected object.
But in case of edit, when I add an option programmatically
var option = new Option(this.device.serial, this.device.id.toString(), true, true);
this.objRef.append(option).trigger('change');
and select the created option again, it just returns id and text of the object and not all the data. If I use custom data adapter it returns all data:
CustomDataAdapter.prototype.updateOptions = function (data) {
let myData: any[] = [data];
this.$element.find('option').remove();
this.addOptions(this.convertToOptions(myData));
}
But I don't like to use custom adapter. Is there any way to convert the object to option same as data adapter so for the next select it contains again all data?
And here is definition of select2. The data is loaded before and I use ajax just for pagination and searching:
var $ddResult = $(_id).select2({
placeholder: '',
selectOnClose: _selectOnClose,
tags: _tags,
allowClear: _allowClear,
minimumResultsForSearch: 5,
ajax: {
transport: function (params, success, failure) {
let pageSize = 30;
let term = (params.data.term || '').toLowerCase();
let page = (params.data.page || 1);
if (timer)
clearTimeout(timer);
timer = setTimeout(function () {
timer = null;
let results = _data
.filter(function (f) {
return f.text.toLowerCase().includes(term);
});
let paged = results.slice((page - 1) * pageSize, page * pageSize);
let options = {
results: paged,
pagination: {
more: results.length >= page * pageSize
}
};
success(options);
}, params.delay);
}
},
})