我有一个自动完成的文本框,其中显示邮政编码,城市,州。目前,当我开始输入邮政编码例如55414时,自动补全功能开始工作,并开始显示相关的邮政编码、城市和州。但是如果我开始输入一个城市名称,我不知道如何触发自动补全。我想在textbox上同时使用这两个触发器。我试图在规则数组中添加另一个规则,但它不起作用。ZipCodes集合有_id、city、state字段。_id是邮政编码。
Template.search.helpers({ settings : function () { return { position: "bottom", limit: 20, rules: [{ collection: ZipCodes, field: "_id", template: Template.userPill }] } }
提前感谢
发布于 2015-02-07 05:56:04
我认为您正在使用meteor-autocomplete,在这种情况下,您可以使用选择器选项
Template.search.helpers({
settings : function () {
return {
position: "bottom",
limit: 20,
rules: [{
collection: ZipCodes,
field: "_id",
template: Template.userPill,
selector: function(match) {
var regex;
regex = new RegExp(match, 'i');
return {
$or: [
{
'_id': regex
}, {
'city': regex
}
]
};
},
}]
}
}
})
发布于 2016-12-29 13:01:02
我知道这个答案对你来说太晚了,但它可以在未来对某些人有所帮助。只需在服务器端publish
函数中执行此操作即可。
Meteor.publish('ZipCodesPublication', function(selector, options) {
let limitTo = Math.abs(options.limit) > 50 ? 50 : options.limit,
defaultSelector = selector._id,
regEx = defaultSelector.$regex,
regExOptions = defaultSelector.$options,
customSeletor = {
$or: [
{
city: {
$regex: regEx,
$options: regExOptions
}
},
{
_id: {
$regex: regEx,
$options: regExOptions
}
}
]
};
Autocomplete.publishCursor(Clients.find(customSeletor), this);
this.ready();
});
然后在客户机上执行以下操作:
Template.search.helpers({
settings : function () {
return {
position: "bottom",
limit: 20,
rules: [{
collection: 'ZipCodes',
subscription: 'ZipCodesPublication',
field: "_id",
template: Template.userPill
}]
}
}
https://stackoverflow.com/questions/27810827
复制相似问题