JStark
JStark

Reputation: 2788

How do i prevent bootstrap typeahead from double filtering non-matching results from ajax call

I'm using bootstrap typeahead with a jQuery ajax which we will call FILTER#1. In my case, an ajax search like "great tank" or "greattank" both produce a "Great Tank" ajax result but typeahead is 2nd guessing my results and double filtering out (we will call FILTER#2) the second result because of the space. It is also possible that my ajax result back may be totally different than what was typed. I need typeahead to trust that the results provided were valid and display them regardless of an exact lowercase text match.

I'd like to have the following behavior:

  1. If there is a matching word in the results highlight it
  2. If there is a non-matching word display it unhighlighted but leave it in the result list vs. filtering it out.

(1) works OOTB, but (2) is the missing feature I'm looking for. If I can't have (1) and (2) I'd be ok with dropping (1).

Here is a fiddle example: http://jsfiddle.net/PE9mN/

        $("#title2").typeahead({
        source: function( request, response ) {
              var mockResults2 = ["Great Tank War", 
                                  "Great Train Robbery",
                                  "other random result my server produced"];
            response(mockResults2); 
            // I expect this to display both items regardless of $("#title2").val() 
            // In my case, the server was smart enough to realize that 
            // "greattank" may match "great tank"
            // and it even added a 3rd value that I want to display.
            }
           }); 

Thanks.

Upvotes: 7

Views: 1976

Answers (2)

Nuwan Dammika
Nuwan Dammika

Reputation: 587

you can do this at initialization as below

  //Typeahead autocomplete for address
    $('input.typeahead').typeahead({
        ajax: {
           .....
        },
        matcher: function () { return true; }
    });

Upvotes: 0

mccannf
mccannf

Reputation: 16659

If you add the following option to typeahead:

  matcher: function(item) {
      return true;
  }

Then no "second" filtering will apply.

Upvotes: 14

Related Questions