[英]Meteor: Regex not working for selector of aldeed:tabular
我正在使用Meteor和aldeed:tabular包來顯示“聯系人”集合的表。 我在表格上方從A到Z設置了按鈕,以便我可以選擇一個字母,並且僅列出“ firstName”以所選字母開頭的聯系人。 我使用表的“選擇器”屬性來執行此操作。
{{> tabular table=ContactsTable.Contacts id="contactsTableID" selector=selector class="table table-bordered table-condensed table-striped"}}
當我單擊一個字母時,我將一個值存儲到這樣的會話變量中...
contacts.html
<button type="button" class="btn btn-default selectLetterA">A</button>
contacts.js
Template.contacts.events = {
'click .selectLetterA': function(e) {
e.preventDefault();
Session.set('selectedLetter','a');
}
};
Template.contacts.helpers({
selector: function (){
if (Session.get('selectedLetter') != '') {
if (Session.get('selectedLetter') == 'all') {return {createdBy: Meteor.userId()}} else if
(Session.get('selectedLetter') == 'a') {return {firstName: {'$regex': /^a/i }}} else if
(Session.get('selectedLetter') == 'b') {return {firstName: {'$regex': /^b/i }}} else if
...
}
} else {
return {createdBy: Meteor.userId()}; //If the selectedLetter is blank, return all contacts created by current user.
}
}
});
使用以上語法,單擊字母時,服務器上出現以下錯誤...
子tabular_getInfo id hyD5bp2r6F2YuzoMa的異常MongoError:無法規范化查詢:BadValue $ regex必須為字符串
因此,如果我嘗試將語法更改為此,則錯誤消失了,但是單擊字母不會在表上返回任何內容。
return {'firstName': {'$regex': '/^a/gi'}};
我也嘗試了以下語法...
return {firstName:/^a/gi};
return {firstName: {'$regex': '/^a/', '$options': 'i'}}
顯然第二個應該工作,但這不適合我。
我還驗證了通過在控制台中使用它肯定應該返回的數據(我看到返回了5個文檔)...
Contacts.find( { "firstName": { "$regex": /^a/gi } } ).fetch()
謝謝
在流星論壇上回答了這個問題... https://forums.meteor.com/t/regex-not-working-for-selector-of-aldeed-tabular/17244/2?u=serks
保持正則表達式為字符串而不包含正斜杠,因此應執行以下操作:
return {
firstName: {
$regex: '^a',
$options: 'i'
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.