Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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);
  });
    })   
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda