简体   繁体   中英

Filter by date range

In Ember it is easy to filter an array where you are looking for matching values ( Only return name == "John) What I can't figure out is how to filter with a greater than or less than ( Return all objects whose startDate is before today

In my app I have a collection of deliverables. I want to divide these deliverables into three categories: Due within ten days, Past due, and then the rest.

I found the following example in another SO post, but can't figure out how to use it to accomplish my goal

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

You can just do:

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

This should return an array of objects within your defined date range. Should work in 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'); }); }) 

With ES6 you could even do something like this:

 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')); }) 

If you have a simpler requirement, computed.filterBy() might be right for you. https://emberjs.com/api/classes/Ember.computed.html#method_filterBy

Also helpful: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM