流星:正则表达式不适用于 aldeed:表格的选择器

Meteor: Regex not working for selector of aldeed:tabular

本文关键字:表格 选择器 aldeed 适用于 正则表达式 不适用 流星      更新时间:2023-09-26

我正在使用Meteor和aldeed:tabular包来显示"联系人"集合的表格。我已经在表格上方设置了从A到Z的按钮,以便我可以选择一个字母,并且只列出"名字"以所选字母开头的联系人。我正在使用表的"选择器"属性执行此操作。

{{> tabular table=ContactsTable.Contacts id="contactsTableID" selector=selector class="table table-bordered table-condensed table-striped"}}

当我单击一个字母时,我将一个值存储到这样的会话变量中......

联系人.html

<button type="button" class="btn btn-default selectLetterA">A</button>

联系人.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 Mongo的异常:无法规范化查询:错误值$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'
  }
}