26

以下の例を参照してください。

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

typeahead.js (0.10.1) で複数のプロパティ値の一致を検索するにはどうすればよいですか? 理想的には、全体data( data.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);
 },

全体の例:

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>'
    }
});
4

3 に答える 3

7

私はここに解決策を実装しました:

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

ローカル データソースがあるため、個々のデータセットを作成して、複数のデータ プロパティを照合できるようにする必要があります。例えば

$('#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()
});
于 2014-02-18T22:58:39.153 に答える