简体   繁体   English

Twitter Typeahead.js如何返回字符串中所有匹配的元素

[英]Twitter Typeahead.js how to return all matched elements within a string

By default twitter typeahead.js returns only elements matched in the begining of a string, for example: 缺省情况下,twitter typeahead.js仅返回在字符串开头匹配的元素,例如:

source: ['type','typeahead','ahead'] 来源:['type','typeahead','ahead']

query: 'type' 查询:“类型”

returns: 'type' and 'typeahead' 返回:'type'和'typeahead'

-- -

query: 'ahead' 查询:“提前”

returns: 'ahead' 返回:'ahead'

I want it to return 'ahead' and 'typeahead' 我希望它返回“ ahead”和“ typeahead”

my code: 我的代码:

var clients = new Bloodhound({
    datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: '/clients.json',
        filter: function(list) {
            return $.map(list, function(value) { return { name: value }; });
        }
    }
});

clients.initialize();

$('.client').typeahead(null, {
    displayKey: 'value',
    source: clients.ttAdapter(),
    minLength: 1,
});

There is already a question about it but i didnt understand the answer. 已经有一个问题,但我不明白答案。

I found a solution... the problem was that I was so used to bootstrap2 typeahead that I wasn't understanding the datumTokenizer thing. 我找到了解决方案...问题是我习惯于提前启动bootstrap2,以至于我不了解datumTokenizer的知识。 If someone else find it hard to understand, I will put a little description below: 如果其他人难以理解,我将在下面进行一些说明:

queryTokenizer: array of words you are querying, if you query for 'test abcd' it will transform the string into ['test','abcd'] and than look for matches with those two words. queryTokenizer:要查询的单词数组,如果查询“ test abcd”,它将把字符串转换为['test','abcd'],然后查找与这两个单词匹配的单词。

datumTokenizer : array of words it will be matched with queryTokenizer. datumTokenizer:将与queryTokenizer匹配的单词数组。 Each item from your JSON will have a set of words to be matched. JSON中的每个项目都会有一组要匹配的单词。

So if you have a source: 因此,如果您有资料来源:

['good test','bad test'] [“好测试”,“坏测试”]

and query for 'est'. 并查询“ est”。 You need to make datumTokenizer return an array containing 'est' , something like: 您需要使datumTokenizer返回包含'est'的数组,类似于:

['good','test','ood','od','test', 'est', 'st'] for the first item 第一项的['good','test','ood','od','test','est','st']

['bad','ad','test', 'est', 'st'] for the second item 第二项的['bad','ad','test','est','st']

Bellow is the code I wrote, I don't know if its the optimal thing for it, but I think it will help anyway: 贝娄是我编写的代码,我不知道它是否是最合适的,但是我认为它还是有帮助的:

new Bloodhound({
    datumTokenizer: function(d) {
        var test = Bloodhound.tokenizers.whitespace(d.value);
            $.each(test,function(k,v){
                i = 0;
                while( (i+1) < v.length ){
                    test.push(v.substr(i,v.length));
                    i++;
                }
            })
            return test;
        },
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    limit: 10,
    prefetch: {
        url: '/lista.json',
        ttl: 10000
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM