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

195
Visualizações
How can you return an explicit "No results found" value to select2 ajax search?

I am using the select2 jQuery dropdown box plugin (latest version), and have an issue where I'm allowing the user to enter an Employee ID to return the names of matching employee(s). This works.

In all searches, I return a value (the "status" of the ajax call, and the employee array if found). I suspect this may be confusing select2 into always thinking there are results.

When there are no actual employees returned, how can I explicitly tell select2 that no results were found so it displays that message?

Currently, I get a nasty jQuery error when no results are found:

jquery-3.6.0.min.js:2 jQuery.Deferred exception: Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length')
    at e (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:39169)
    at Object.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:38243)
    at e (https://code.jquery.com/jquery-3.6.0.min.js:2:30038)
    at t (https://code.jquery.com/jquery-3.6.0.min.js:2:30340) undefined
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Was going to delete as I found the answer shortly after, but figured this might help future coders out.

Simply return an explicit "results" value with an empty json object in the format select2 expects like below:

return { results: [] };
about 4 years ago · Juan Pablo Isaza 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