键入提前.js显示所有结果,而不仅仅是匹配结果

typeahead.js showing all results instead of just matching results

本文关键字:结果 不仅仅是 js 显示      更新时间:2023-09-26

我在获取 typeahead 时遇到问题.js只返回与输入的查询匹配的结果。例如,如果我在我的公司搜索栏中输入"Facebook",它将返回所有公司("雅虎"、"谷歌"等),即使其中大多数与查询不匹配。我没有对数据进行任何服务器端处理。我的基准分词器函数应该负责此过滤吗?

另外,我注意到每次修改查询时,它都会为每个基准面输入 filter() 函数。因此,当我将查询从"G"更改为"Go"时,filter: 函数 (companies_list) 中的 console.log() 语句将打印 3000 次。

这是我的代码:

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()
  });

以及我的网址返回的示例:

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

正在使用远程,因为预取绝对不适合我。它只给了我建议[对象对象],这毫无意义。我想在初始化时使用预取/远程加载整个 .json 文件,并且不向服务器发出任何进一步的请求。所以我认为预取是我更好的选择(小文件,77kB),但它根本不起作用。

非常感谢 v 的帮助!

我认为你缺少"准备"。尝试像我一样的事情

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,
});

或者,你可以在那里看看我的帖子。它仅返回用户在文本框中输入的结果。我的堆栈溢出帖子

我也有这个问题。问题是远程 URL 返回标准列表,无法将查询传递给它,以便它返回与搜索词匹配的筛选子集。因此,筛选器实现必须处理筛选结果,以便它们与查询匹配。