Gazano
Gazano

Reputation: 183

problems with a filter date in angularJS

i have a problems with my filter date , I would like to filter between 2 date but with a condition if the date from is not valid then I start at 2017-01-01 (for that I think it is good) and if the date 'to' is not valid I start Has the current date

Here is my plunker:

http://plnkr.co/edit/8sK29zG7YoPOdntbFfcK?p=preview

Thank you for your help

Upvotes: 1

Views: 54

Answers (2)

Hosar
Hosar

Reputation: 5292

Your filter could be something like this :

app.filter("myfilter", function($filter) {

      return function(items, from, to) {

      const testFrom = Date.parse(from);
      const testTo = Date.parse(to);      


      if (!testFrom){
        console.log('Not valid');
        from = moment('2017-01-01');
      }

      //here it does nt work
      if (!testTo){
       to = moment();              
      }      


      const valids = items.reduce((acc,val) => {
         const date = moment(val.dateenvoi);
         if(date.isSameOrAfter(from) && date.isSameOrBefore(to))
          acc.push(val)        
        return acc;
      },[]);      
      return valids;      
      };
});

Upvotes: 1

Gaurav Kumar Singh
Gaurav Kumar Singh

Reputation: 1570

Check this

No need for custom filter for this functionality, you can use angular predefined filter for this like

<input type="text" name="S_Date" ng-model="filter.dateenvoi"/>

and

<tr ng-repeat="x in myData | filter: filter">

working Plunker

Upvotes: 0

Related Questions