【问题标题】:Filter by date range按日期范围过滤
【发布时间】:2024-01-15 12:54:01
【问题描述】:

在 Ember 中,很容易过滤您正在寻找匹配值的数组(仅返回名称 ==“约翰)我不知道如何过滤大于或小于(返回所有对象开始日期在今天之前

在我的应用程序中,我收集了可交付成果。我想将这些可交付成果分为三类:十天内到期,逾期到期,然后是其余的。

我在另一个 SO 帖子中找到了以下示例,但不知道如何使用它来实现我的目标

filterComputed: function() {
  return this.get('content').filter(function(item, index, enumerable){
    return item.firstName == 'Luke';
  });
}.property('content.@each')

【问题讨论】:

标签: date ember.js filter


【解决方案1】:

你可以这样做:

this.get('content').filter(function(item){
    return item.get('someProperty') > someVar;
});

【讨论】:

    【解决方案2】:

    这应该返回您定义的日期范围内的对象数组。应该在 Ember ^2.x 中工作。

    filterComputed: computed('content.@each', 'startDate', 'endDate', function() {
      return this.get('content').filter(function(item) {
        var contentDate = item.get('date'); // expecting item to have a date property  
        return contentDate > this.get('startDate') && bookingDate < this.get('endDate');
      });
    })

    使用 ES6,您甚至可以执行以下操作:

    filterComputed: computed('content.@each', 'startDate', 'endDate', function() {
      return this.get('content').filter(item => item.get('date') > this.get('startDate') && item.get('date') < this.get('endDate'));
    })

    如果您有更简单的要求,computed.filterBy() 可能适合您。 https://emberjs.com/api/classes/Ember.computed.html#method_filterBy

    也有帮助:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

    【讨论】: