Twitter中多属性对象的子字符串匹配

Substring matching for multi property object in Twitter Typeahead

本文关键字:字符 字符串 串匹配 对象 属性 Twitter      更新时间:2023-09-26

在进入子字符串匹配器之前,数据是这样的:

var datasource = [
{name: 'Activity One', id: '1'},
{name: 'Activity Two', id: '2'},
{name: 'Activity Three', id: '3'},
{name: 'Activity Four', id: '4'}
];

当它是一个普通的值数组

var datasource =  { 'Activity One', 'Activity Two',...

使用twitter typeahead来正确匹配很简单,但是现在有多个属性,我不确定子字符串匹配器应该如何工作。

这是如何初始化typeahead:

$(control)
    .typeahead({
            hint: true,
            highlight: true,
            minLength: 0
        },
        {
            displayKey: "name",
            name: "name",
            source: substringMatcher(datasource)
        });

现在,字符串在初始单击时正确匹配,但不能再使用子字符串匹配器进行匹配。

这是子字符串匹配器:

var substringMatcher = function (strs) {
return function findMatches(q, cb) {
    var matches = [];
    var substrRegex = new RegExp(q, 'i');
    $.each(strs,
        function (i, str) {
            if (substrRegex.test(str)) {
                matches.push(str);
            }
        });
    cb(matches);
};};

我需要改变什么?我不知道如何引用*.name属性。

Bloodhound进行子字符串匹配。至于访问属性,使用

var ds = datasource.map(function(o) { return o.name; });