I have Multiple Value Select2 Which have options generated from MySQL using inline PHP when we submit the form the select2 is encoded into json_encode ["43", "23", "32"] What I want is when user want to edit I make a follow as the user clicks on the button and ajax call is sent to MySQL and in response, we get all values and what I need is show json_encoded array into Select2, it works fine with single value but not working with multiple
If the value is single value its work but in my case its json_array
$(".select2Custom").select2({theme: 'bootstrap4'}).val(data.product_id).trigger('change');
2
Answers
Thanks, @Arman Your Solution is Not Giving Me 100% Solution But Give Me the Vision to Solve My Problem like your hint to convert the array into Select2 acceptable Array :) Here is Solution
I Got JSON_ENCODED Array From Ajax Response
["33", "44", "23"]
What I Need is to Show those Options which have id in an arrayand here is Solution
//I Just Convert Ajax Response into proper JSON and its Works
You should make same format json as the select2 accepts like:
So convert your initial json with this method:
check this fiddle
Edit: Changed fiddle so we select2 can render this array
["32", "44", "40"]