I have created a filter function that is applied to elements in a list. It works when I use it on the page with a click event, like this:
<button (click)="FilterFn5()"> do </button>
However, it does not work when I put the function in the ngOnInit lifecycle hook. Here is how my ngOnInit function looks:
ngOnInit(): void {
this.BuildingNameFilter="B"
this.BuildingRoomsFilter="2"
this.BuildingFloorFilter="0"
this.refreshBldList();
this.FilterFn5();
}
In this function:
FilterFn5(){
var BuildingNameFilter = this.BuildingNameFilter;
var BuildingFloorFilter = this.BuildingFloorFilter;
var BuildingRoomsFilter = this.BuildingRoomsFilter;
this.BuildingList = this.BuildingListWithoutFilter.filter( (el:
{ BuildingId:
{ toString: () => string; }; BuildingFloor: { toString: () => string; };
BuildingName: { toString: () => string; }
ApartmentRooms: { toString: () => string; }; }) =>{
return el.ApartmentRooms.toString().toLowerCase().includes(
BuildingRoomsFilter.toString().trim().toLowerCase()
)
&& el.BuildingFloor.toString().toLowerCase().includes(
BuildingFloorFilter.toString().trim().toLowerCase()
)&&
el.BuildingName.toString().toLowerCase().includes(
BuildingNameFilter.toString().trim().toLowerCase()
)
});
}
The list that is being filtered is BuildingList
and the function this.refreshBldList();
is used to populate the list with elements
refreshBldList(){
this.service.getBuildingList().subscribe(data=>{
this.BuildingList=data;
this.BuildingListWithoutFilter=data;
});
}