【问题标题】:AutoComplete with any part of the word自动完成单词的任何部分
【发布时间】:2014-02-25 00:21:29
【问题描述】:

我正在寻找的是不仅显示单词开头的建议,而且如果它符合单词的任何部分。

例如 如果我有一个单词列表 [Manimal, animal, person, erson]

当我输入 animalani 它应该同时显示 Manimal 和 animal 或者如果我输入 son 它应该同时显示人和儿子

如何使用 typeahead 和 Bloodhound 做到这一点?

我试过了

var jobCodes = new Bloodhound({
  datumTokenizer: function (d) { return [d.JobName]; },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: "/api/jobservice/getjobcodes"
});

Or 

var jobCodes = new Bloodhound({
  datumTokenizer: function (d) { return Bloodhound.tokenizers.nonword(d.JobName); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: "/api/jobservice/getjobcodes"

});

【问题讨论】:

标签: javascript jquery typeahead.js twitter-typeahead bloodhound


【解决方案1】:

创建/获取要检查的字符串数组,然后定义一个函数,该函数将字符串数组作为第一个参数,将子字符串作为第二个参数。

然后创建一个空数组来保存结果(如果有的话)。

遍历数组中包含的每个字符串,如果其中任何一个包含子字符串,则将该字符串推送到结果数组中。

最后,如果有结果,返回它们。否则返回“无结果”。

var phrases = ["lazy", "little", "laughing", "ladies"];
var checkStringsForSubString = function(str, subStr) {
    var results = [];
    for (var i = 0; i < phrases.length; i++) {
        if (str[i].indexOf(subStr) != -1) {
            results.push(str[i]);
        }
    }
    if (results.length > 0) {
        return results;
    } else {
        return "no results";
    }
};
checkStringsForSubString(phrases, "la");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多