Twitter Typeahead 中多 属性 对象的子字符串匹配
Substring matching for multi property object in Twitter Typeahead
因此在进入子字符串匹配器之前数据看起来像这样:
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 来正确匹配很简单,但是现在有多个属性,我不确定子字符串匹配器应该如何工作。
这是初始化预输入的方式:
$(control)
.typeahead({
hint: true,
highlight: true,
minLength: 0
},
{
displayKey: "name",
name: "name",
source: substringMatcher(datasource)
});
现在,字符串 on the initial click, but can't match it anymore 使用子字符串匹配器正确匹配。
这是子字符串匹配器:
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; });
因此在进入子字符串匹配器之前数据看起来像这样:
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 来正确匹配很简单,但是现在有多个属性,我不确定子字符串匹配器应该如何工作。
这是初始化预输入的方式:
$(control)
.typeahead({
hint: true,
highlight: true,
minLength: 0
},
{
displayKey: "name",
name: "name",
source: substringMatcher(datasource)
});
现在,字符串 on the initial click, but can't match it anymore 使用子字符串匹配器正确匹配。
这是子字符串匹配器:
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; });