I have multiple different pipes that I would like to toggle on and off if the user wants to filter their data by some different criterias. How would I activate/deactivate the pipes currently used in a search or build a single pipe that behaves differently depending on what buttons the user have clicked on?
For example two pipes/filters would look like this...
//cloud.pipe.ts
import {Pipe} from '@angular/core';
import {Hero} from './hero';
@Pipe({
name: 'Cloud'
})
export class CloudPipe{
transform(value) {
if (value == null) {
return null;
}
return value.filter(hero => {
return hero.cloud === true;
});
}
}
//location.pipe.ts
import {Pipe} from '@angular/core';
import {Hero} from './hero';
import { HeroService } from './hero.service';
import { HeroesComponent } from './heroes.component';
@Pipe({
name: 'Location'
})
export class LocationPipe{
transform(value) {
if (value == null) {
return null;
}
return value.filter(hero => {
return hero.location < 500;
});
}
}
Then I would like to have the user toggle different filter buttons and add/remove pipes to the list. What's the best approach for something like this?
<!--Toggle what pipes should be used in search-->
<!--For example how can I construct the "updatePipe" function for doing this?-->
<button id="activateCloud" (click)="updatePipe()"></button>
<button id="activateLocation" (click)="updatePipe()"></button>
<!--Is it possible to have: neither of the pipes active, both at the same time or just one at the time? How can I do this?-->
<div *ngFor="let hero of heroes | Cloud | Location ></div>
I'd rather not have everything in the same pipe as I would like to extend each pipe to do more in the future. So each pipe should "be it's own" and and work independently from one another but at the same time work in conjunction with other pipes when necessary.