Hi ,
I am trying to pre-populate a select2 from a remote data source.
My data source is not a standard ajax ( it is a graphql datasource) and I am using processResults and templateResults
How would I best pre-populate the select2 dropdown ?
Is there a way to call the transport function, with the params argument I need for the initial population of the dropdown.
What would I pass as success and failure functions in this case ?
below is my select2 ajax setup
thanks
ajax: {
transport: (params, success, failure) => {
if (params.data['term']) {
if (params.data['term'].length > 1) {
ClassCode.get(params.data['term']).then(data => { success(data.classCodes) })
}
}
return null
},
processResults: function (data) {
data = data.map((x) => { return { id: x.code, text: x.code + "|" + x.descriptions[0].description } })
return {
results: data
};
},
},
templateSelection: function (state) {
return state.id
},
templateResult: function (data) {
var r = data.text.split('|');
var result = $(
'<div class="row">' +
'<div class="col-md-3">' + r[0] + '</div>' +
'<div class="col-md-9">' + r[1] + '</div>' +
'</div>'
);
return result;
}