I have created a filter and sort pipes in Angular and chained it as below. But the pipe chaining is not working. If I use only a single pipe it works but if I use two pipes like below only the filter functionality works sort pipe does not work. Can somebody tell me what is the mistake that I am doing?
<div class="grid-container">
<div class="grid-item" *ngFor="let prod of (productsList.products | sort:sortProductSelected | filterOnPrice:priceRange )">
<div class="card" style="width: 18rem;border: 0px;">
<img src="{{prod.thumbnail}}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{prod.brand}}</h5>
<p class="card-text">${{prod.price}}</p>
<p class="card-text">{{prod.description}}</p>
<a target="_blank" class="btn btn-primary" (click)="viewProduct(prod.id)">View Product</a>
</div>
</div>
</div>
</div>
</div>
Sort pipe
export class SortPipe implements PipeTransform {
transform(value: any[], ...args: any): any[] {
if(args){
switch(args[0]){
case 'Price low to hight':
value.sort((a,b)=>{
if(a.price<b.price) return -1;
else if(a.price>b.price) return 1;
return 0;
console.log("value");
})
break;
case 'Price High to Low' :
value.sort((a,b)=>{
if(a.price>b.price) return -1;
else if(a.price<b.price) return 1;
return 0;
})
break;
case 'Ratings':
value.sort((a,b)=>{
if(a.rating<b.rating) return -1;
else if(a.rating>b.rating) return 1;
return 0;
})
break;
case 'Discounts' :
value.sort((a,b)=>{
if(a.discountPercentage<b.discountPercentage) return -1;
else if(a.discountPercentage>b.discountPercentage) return 1;
return 0;
})
break;
default: return value;
}
}
return value;
}
}
Filter Pipe
export class FilterOnPricePipe implements PipeTransform {
transform(value: any[], ...args: any): any[] {
let FliteredArray:any[] =[];
FliteredArray = value.filter((a)=>{
console.log(a.price);
console.log(FliteredArray);
return a.price<=args[0];
})
console.log(FliteredArray);
return FliteredArray;
}
}
Its best to use the
filterPipefirst then perform thesortPipewhen I did this it worked fine, since we are removing the unnecessary values and sorting it will improve the performance!Also I guess the input reference did not change so you were getting this issue!
Because when I changed the line 45 in
sortPipeit started working fine (without swapping the pipes), so I guess the reference of the array needs to be updated for the pipe to be triggered.I think you should perform the filter first and then sort, since its makes sense and no need to update the reference!
Below is a working example!
ts
stackblitz