logo
down
shadow

Sort typeahead suggestions with the exact input at top


Sort typeahead suggestions with the exact input at top

By : user2952661
Date : November 19 2020, 01:01 AM
Hope that helps You want something along these lines. This will move the exact match to the top. You will want to continue modifying the sorter code to handle string case, whitespace, and how you want non perfect but close matches to be handled.This should get you started.
code :
        var suggestion = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: data,
            limit: 20,
            sorter:function(a, b) { 

                     //get input text
                 var InputString=   $(Selector).val();

                     //move exact matches to top
                 if(InputString==a.value){ return -1;}
                 if(InputString==b.value){return 1;}

                      //close match without case matching
                 if(InputString.toLowerCase() ==a.value.toLowerCase()){ return -1;}
                 if(InputString.toLowerCase()==b.value.toLowerCase()){return 1;} 

                 if( (InputString!=a.value) && (InputString!=b.value)){

                      if (a.value < b.value) {
                         return -1;
                      }
                      else if (a.value > b.value) {
                         return 1;
                      }
                      else return 0;
                 }
              },
          });


Share : facebook icon twitter icon
Map typeahead suggestions after they are fetched

Map typeahead suggestions after they are fetched


By : Yao Du
Date : March 29 2020, 07:55 AM
may help you . You could use the filter(parsedResponse) to do the parsing of the response yourself. You can see an example (to an unrelated answer) here.
Typeahead not working: not showing typeahead suggestions when I type

Typeahead not working: not showing typeahead suggestions when I type


By : KondratevPavel
Date : March 29 2020, 07:55 AM
this will help I will show two different solutions. Both work. One is a lot shorter than the other.
Solution 1:
code :
var agency_names = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: 'agencies/get_unique_agency_names'
});

$('.typeahead.name_input').typeahed(null, {
  name: 'agency_names',
  source: agency_names
});
var agencies_ary = [];
$.ajax({
    url: "agencies/get_unique_agency_names",
    type: "GET",
    success: function(data){
        agencies_ary = data;
        console.log(agencies_ary[0]);
    }
}).done(function(){

    var substringMatcher = function(strs) {
        return function findMatches(q, cb) {
            var matches, substringRegex;
            matches = [];
            substrRegex = new RegExp(q, 'i');
            $.each(strs, function(i, str) {
                if (substrRegex.test(str)) {
                    matches.push(str);
                }
            });
            cb(matches);
        };
    };


    $('.typeahead').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
        },
        {
            name: 'agencies_ary',
            source: substringMatcher(agencies_ary)
        });


});
Providing suggestions via Typeahead.js

Providing suggestions via Typeahead.js


By : suresh
Date : March 29 2020, 07:55 AM
seems to work fine I have an app that uses Bootstrap, jQuery, Typeahead.js and Azure Search. I'm trying to use the suggestions feature. In an attempt to do this, I have the following: , A couple of things seem off to me. First this:
code :
$('#searchField').typeahead([{
  minLength: 5,
  name: 'suggestions',
  source: engine
}]);
$('.typeahead').typeahead(null,{
  minLength: 5,
  source: engine
});
var engine = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'https://my-app.search.windows.net/indexes/my-index/docs/suggest?api-version=2015-02-28',
    prepare: function (query,settings) {
      settings.type = 'POST';
      settings.contentType = 'application/json; charset=UTF-8';
      settings.data = { search: query, suggesterName: 'mySuggestions', top: 10 };                                                        
      return settings;
    }
  }
});
Why are the typeahead suggestions undefined?

Why are the typeahead suggestions undefined?


By : John
Date : March 29 2020, 07:55 AM
Any of those help Hopefully this is not a duplicate of: Why does bloodhound.get() return undefined? , The local array must contain objects, not strings themself.
Twitter typeahead default suggestions: how to get the first suggestions in the prefetched array?

Twitter typeahead default suggestions: how to get the first suggestions in the prefetched array?


By : John Weinberg
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am using Twitter Typeahead and I would like to offer some values to the user when the input box gets focus, before anything was typed. , For example, you want to display 2 first items in default
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org