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

87
Visualizações
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 à 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