Typeahead.js shows all results, not just results

I am having trouble getting typeahead.js to return only results that match the query I entered. For example, if I type “Facebook” in the company search bar, it will return all the companies (“Yahoo”, “Google”, etc.), although most of them do not match the query. I do not process data on the server side. Should my datumTokenizer function take care of this filtering?

In addition, I notice that every time I change the request, it introduces a filter () function for each base element. Therefore, when I change the request from "G" to "Go", the console.log () operator in the filter: function (company_list) function will print 3,000 times.

Here is my code:

var companies = new Bloodhound({
    datumTokenizer: function (datum) {
      return Bloodhound.tokenizers.whitespace(datum.name);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
      url: '/json/company_list.json',
      filter: function (companies_list) {
        // Map the remote source JSON array to a JavaScript object array
        return $.map(companies_list, function (company) {
          console.log('mapping')
          return {
            name: company.name
          };
        });
      }
    }
  });

  // Initialize the Bloodhound suggestion engine
  var promise = companies.initialize();
  promise.done(function() {console.log('Bloodhound initialized!')});

  // passing in `null` for the `options` arguments will result in the default
  // options being used
  $('#form-company').typeahead(null, {
    name: 'companies',
    displayKey: 'name',
    // `ttAdapter` wraps the suggestion engine in an adapter that
    // is compatible with the typeahead jQuery plugin
    source: companies.ttAdapter()
  });

And an example of returning my URL:

[{"name": "Yahoo"}, {"name": "Sanchai Technologies "}, {"name": "Oliver Wyman"}, {"name": "University of Oregon"}, ...]

remote, prefetch . [object Object], . prefetch/remote .json . , - ( , 77 ), .

!

+4
2

, "". - ,

var information = {
    url: urlRoot,
    prepare: function (query, settings) {
        settings.type = "POST",
        settings.url += '?prefix=' + query;
        return settings;
    }
};
var names = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('navn'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,

    remote: information,

});

, . , . fooobar.com/questions/1570922/...

0

. , URL- , , , . - , .

0

Source: https://habr.com/ru/post/1570921/


All Articles