I have my first select2 that I pull data into from remote ajax search with multiple options allowed:
$("#EmailSentToGroup").select2({
ajax: {
url: "/trip-search",
dataType: "json",
delay: 250,
data: function(params) {
return {
q: params.term
};
},
processResults: function(data, params) {
var resData = [];
data.forEach(function(value) {
resData.push(value);
})
return {
results: $.map(resData, function(item) {
return {
text: item.TripName,
id: item.Id
}
})
};
}
//,cache: true
},
placeholder: "Select Trip(s)...",
minimumInputLength: 3
})
with data return like this:
[
{"id":"123","text":"Trip Name 1"},
{"id":"456","text":"Trip Name 2"},
{"id":"789","text":"Trip Name 3"}
]
I then have a second select2 that I want to populate based on the selection(s) that are in the first select2. I initialize like this on ready: $("#NewSelect2").select2();
onchange or onselect of the first select2, I want the data to be updated in the second. I want to have optgroups to be able to group my data returned by trip for ease of viewing.
$('#EmailSentToGroup').on('change select', function() {
updatePackages();
});
Now updatePackages is like this, the part for processResults
or success
is what I cannot figure out.
- The format of the JSON response
- How to process these results into meaningful data select2 can understand & display.
function updatePackages(){
$("#NewSelect2").select2({
ajax: {
url: "/data/data_select.asp?ids=" + $("#EmailSentToGroup").val(),
type: "POST",
dataType: "json'",
processResults: function (data) {
//THIS IS THE PART I CANNOT FIGURE OUT!!!
}
}
});
}
I have seen some of the other similar posts, but they do not add in the other complexities I am trying to do. (Populatate a second select based on selection a 1st select - #3 by galactica)
any help would be greatly appreciated as I have spent the better part of a day on this already!
Thank you.