如何在模板中过滤 Meteor 集合

How to filter a Meteor collection within a template

我有一个模板,我在其中填充所有传输

Template.listTransfers.helpers({
  transfers: function () {
    var thisId = Meteor.userId();
    return Transfers.find({userId: thisId}, {sort: {timeCreated: -1}});
  },
});

我在模板中还有一个点击事件需要过滤掉有发票的转账return Transfers.find({userId: thisId, 'invoice' : {$exists : 1}}, {sort: {timeCreated: -1}});

而且我有这样的模板拉动传输

 {{#each transfers}}
   {{> transferItem}}
 {{/each}}

有没有办法在该语句中执行此操作,或者我是否需要创建单独的路由?

您可以像这样在模板实例上使用 ReactiveVar:

Template.listTransfers.created({
  // Initialize a reactive variable on the template instance
  this.showInvoices = new ReactiveVar(true);
});

Template.listTransfers.helpers({
  transfers: function () {
    var thisId = Meteor.userId();
    var showInvoices = Template.instance().showInvoices;

    if (showInvoices.get()) {
      return Transfers.find({userId: thisId}, {sort: {timeCreated: -1}});
    } else {
      return Transfers.find({userId: thisId, 'invoice' : {$exists : 1}}, {sort: {timeCreated: -1}});
    }
  },
});

Template.listTransfers.events({
  "click button.hide-invoices": function () {
    var showInvoices = Template.instance().showInvoices;

    // Toggle the showInvoices var
    showInvoices.set(! showInvoices.get());
  }
})