简体   繁体   English

Typeahead.js - 搜索多个属性值

[英]Typeahead.js - Search in multiple property values

Please see example below. 请参阅下面的示例。

JSFiddle: http://jsfiddle.net/R7UvH/2/ JSFiddle: http//jsfiddle.net/R7UvH/2/

How do I make typeahead.js (0.10.1) search for matches in more than one property value? 如何让typeahead.js (0.10.1)在多个属性值中搜索匹配项? Ideally, within whole data ( data.title , data.desc and in all data.category[i].name ) 理想情况下,在整个datadata.titledata.desc和所有data.category[i].name

 datumTokenizer: function(data) {
     // **search in other property values; e.g. data.title & data.desc etc..**
     return Bloodhound.tokenizers.whitespace(data.title);
 },

Whole example: 整个例子:

var data = [{
    title: "some title here",
    desc: "some option here",
    category: [{
        name: "category 1",
    }, {
        name: "categoy 2",
    }]
},
{
    title: "some title here",
    desc: "some option here",
    category: [{
        name: "category 1",
    }, {
        name: "categoy 2",
    }]
}];

var posts = new Bloodhound({
    datumTokenizer: function(data) {
        // **search in other property values; e.g. data.title & data.desc etc..**
        return Bloodhound.tokenizers.whitespace(data.title);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: data
});
posts.initialize();

$('#search-input').typeahead({
    highlight: true
}, {
    name: 'Pages',
    displayKey: 'title',
    source: posts.ttAdapter(),
    templates: {
        header: '<h3>Pages</h3>'
    }
});

Typeahead 0.10.3 added "support to object tokenizers for multiple property tokenization." Typeahead 0.10.3添加了“对多个属性标记化的对象标记化器的支持”。

So, your example becomes 所以,你的榜样就变成了

var posts = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title', 'desc'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: data
});

However, I don't think this will work for properties nested inside, that is, the data.category object in your case. 但是,我认为这不适用于嵌套在其中的属性,即您的情况下的data.category对象。

As a side note, if you are using prefetched data, be sure to clear the local storage first, otherwise the new tokenizer won't take effect (Because datumTokenizer is used when adding to the search index, and if data is already present in localStorage , then the search index will not be updated). 另请注意,如果您使用的是预取数据,请务必先清除本地存储,否则新的tokenizer将不会生效(因为在添加到搜索索引时使用了datumTokenizer ,并且如果localStorage已存在数据,那么搜索索引将不会更新)。 I was stuck on this for a while! 我被困在这一段时间了!

return Bloodhound.tokenizers.whitespace(data.title) returns an array of strings. return Bloodhound.tokenizers.whitespace(data.title)返回一个字符串数组。

So, instead of returning that value: save it (and your other desired values), then concatenate them and return that value... 所以,而不是返回该值:保存它(以及您的其他所需值),然后连接它们并返回该值...

x = Bloodhound.tokenizers.whitespace(data.title);
y = Bloodhound.tokenizers.whitespace(data.desc);
z = Bloodhound.tokenizers.whitespace(data.category[i].name);
return x.concat(y).concat(z);

I've implemented a solution here: 我在这里实现了一个解决方案:

http://jsfiddle.net/Fresh/4nnnG/ http://jsfiddle.net/Fresh/4nnnG/

As you have a local datasource you need to create individual datasets to enable you to match on multiple data properties. 由于您有本地数据源,因此需要创建单独的数据集以使您能够匹配多个数据属性。 eg 例如

$('#search-input').typeahead({
    highlight: true
}, {
    name: 'titles',
    displayKey: 'title',
    source: titles.ttAdapter()
}, {
    name: 'descs',
    displayKey: 'desc',
    source: descs.ttAdapter()
}, {
    name: 'cats',
    displayKey: 'name',
    source: cats.ttAdapter()
});

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

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