Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
AJAX data in template of Select2

I am populating Select2 with AJAX data. My code is like below.

    function formatState(state) {
        if (!state.id) { return state.text; }
        var output = $('<span></span>');   // I would like get data `text[1]` of `processResults` here.
        return output;
    };

    //more code here

    cellEle.html(`<select multiple="multiple"></select>`);
    
    let selectEle = cellEle.children("select").select2({

        //more code here
        
        ajax: {

            //more code here

            processResults: function (data) {                
                var options = [];
                if (data) {
                    $.each(data, function (index, text) {
                        options.push({ id: index, text: text[0]});
                    });
                }
                return {
                    results: options,
                    more: false
                };
            },
        },
        templateSelection: formatState,
    });

I would like get AJAX data text[1] of processResults inside formatState() of templateSelection.

How can I get it ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can pass any extra data via processResults. In your case it can be

var options = [];
if (data) {
  $.each(data.items, function (index, text) {
    options.push({ id: index, text: text[0], text2: text[1] /* <-- extra data */});
  });
}
return {
  results: options,
  more: false
};

Then access it in templateSelection

if (!state.id) {
  return state.text;
}
return $(`<span>${state.text} & ${state.text2}</span>`);

http://jsfiddle.net/moshfeu/3hrupdn1/22/

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda