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

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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