Binho Pirolli
Binho Pirolli

Reputation: 21

Angular2 or newest Date Range Pipe

I looking for all web and not get a solution for my case. I need filter a Json result by date begin and date end.

I using Angular 7, this "dateRangeFilter" its only for e.g.; my code its like this:

<tr *ngFor="let caixa of result | dateRangeFilter: '08/03/2019' , '14/03/2019' ">
            <td>{{caixa.tipo}}</td>
            <td>{{caixa.data}}</td>
            <td>{{caixa.hora}}</td>
            <td>{{caixa.cliente}}</td>
            <td>{{caixa.valor | currency:'BRL':symbol:'1.2-2'}}</td>
 </tr>

Upvotes: 0

Views: 994

Answers (2)

Binho Pirolli
Binho Pirolli

Reputation: 21

Great, Thanks a lot. I make this code and work`s fine.

But date Begin and date End I want get of Inputs Date, and the filter function it`s not read variables:

function dataRange(obj) {
if (obj.id >= this.dateBegin && obj.id <= this.dateEnd) {
return true;
 }
}

But if I put a String Date, run perfect !!

function dataRange(obj) {
if (obj.id >= "2019-03-01" && obj.id <= "2019-03-05") {
return true;
 }
}

How I use a Variable Date ??

Upvotes: 0

jcuypers
jcuypers

Reputation: 1794

It has been deprecated to use pipes for filtering (performance reasons). The better solution is use standard JS/ES6/.. constructs to filter your data before it is bound to your visual representation. You could use some custom filter logic with .filter.

you can find an example on an already existing post: Filter Array in Array by date between 2 dates

extract:

data.series = data.series.filter((item: any) =>
    item.date.getTime() >= fromDate.getTime() && item.date.getTime() <= toDate.getTime()
);

Upvotes: 1

Related Questions