Hi team
I am facing an issue in select2js Multiselect dropdown preselect.
I am trying to find the items using .find() option provided by select2, somehow it’s not removing and preselecting the items
Code Sample:
// getting the already selected values
var selectedvalue = $(ChildControl).val();
// then refreshing the control with new values
$(ChildControl).select2(
{
ajax: {
url: baseUrl,
dataType: ‘json’,
async: true,
data: { search: SearchParam },
processResults: function (data, params) {
var resData = [];
data.forEach(function (value) {
if (value.Value.toLowerCase().indexOf(params.term.toLowerCase()) != -1)
resData.push(value);
});
return {
results: $.map(resData, function (item) {
return {
text: item.Value,
id: item.Value
}
})
};
},
cache: true
},
minimumInputLength: 1
});
// then trying to preselect the values from the saved list to the newly added items
var selectedArray = [];
selectedvalue.forEach(function (value) {
selectedArray.push(value);
});
if ($(ChildControl).find(“option[value=’” + selectedArray + “’]”).length) {
$(ChildControl).val(selectedArray).trigger(‘change’);
//$(ChildControl).select2(‘val’, selectedvalue);
}
Issue:
control is not removing the not found items and preselecting the found items.
Please help to find out what is wrong in my approach
Thank you