I have data in JSON created by Django serialization. I use the example: Select2 Loading remote data but still I getting information in a field that nothing found.
What should change to select2 work with data generated by Django?
JSON Data:
[{
"fields": {
"sku": "8"
},
"model": "catalog.product",
"pk": 8
},{
"fields": {
"sku": "9"
},
"model": "catalog.product",
"pk": 9
}]
Html:
<select class="js-data-example-ajax"><option value="3620194" selected="selected">select2/select2</option></select>
JavaScript:
$('.js-data-example-ajax').select2({
ajax: {
url: "{% url 'catalog.views.product_sku_json' %}",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
});
Answer by kevin-brown on github:
You are going to need to re-map your results to have id and text keys.
https://select2.github.io/announcements-4.0.html#changed-id
And it works thanks Kevin!