'select2 add custom classes using processResults
I used to add custom classes and use it to fill an input according to the selection since I did it directly in the view
example:
<option value="290044" data-nombre="xxxx" data-select2-id="20">0931105/Diego Suarez</option>
but since I had to bring more than 70000 records I do it now from the server side and I don't know how to add the custom classes that I want to appear it appears like this automatically
example
<option value="290044" (here i want to add my custom class) data-select2-id="20">09311055/Diego Suarez</option>
I attach the complete code
$(document).ready(function () {
$("#cliente").select2({
language: "es",
minimumInputLength: 2,
ajax: {
url: "/Clientes/jsonclientes",
dataType: "json",
delay: 250,
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.NIF20 + "/" + item.NOMBRECLIENTE,
id: item.CODCLIENTE,
};
}),
};
},
cache: true,
},
});
$("#cliente").change(function () {
let selected = $(this).find("option:selected");
direccion = selected.data("direccion");
$("#direccione").val(direccion);
});
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
