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

89
Vistas
typeahed perform ajax call only if string contains space

I'm trying to use/configure typeahead to just perform the ajax call if the typed string contains space. To be more specific, I would like to display fulladdresses but at my API I would like to avoid the street number part. I was trying to achieve that using a function assigned to the 'source' property:

   $("#select").typeahead({
            minLength: 3,
            source: function (request, response) {
                var streetName = '';

            for (i = 1; i < streetParts.length; i++) {
                streetName += streetParts[i] + ' ';
            }

            if (streetName.length < 3) return null;

            $.ajax({
                url: "/api/Search/suggest/",
                data: { "input": streetName.trim() },
                type: "GET",
                hint: true,
                contentType: "json",
                success: function (data) {
                    items = [];
                    map = {};
                    $.each(data, function (i, item) {
                        var id = item.document.id;
                        var name = item.text;
                        map[name] = { id: id, name: name };
                        items.push(name);
                    });
                    response(items);
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        updater: function (item) {
            //If simultaneously want to update value somewhere else
            $("#selected").html(map[item].id);
            return item;
        }
    });

even though I can see the request at the network tab is fine, the div with the results are not being rendered. If I remove the if statement from the source function it works fine. Is it the right way to cancel the execution of the source function (returning null)? Is there any other property or way to achieve what I'm trying?

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