#I need to filter data in the backend in my code

4 messages · Page 1 of 1 (latest)

hasty yew
#

`filterData: BlacklistFilterData = {
searchText: '',
page: 1,
pageSize: 10,
pageIndex: 0,
statusId: 154,
sourceId: 1061
};
displayedColumns: string[] = [
...
];

dataSource = new MatTableDataSource<BlacklistData>();

searchControl = new FormControl();
length: number = 0;
totalCount = 0;
private blacklistService = inject(BlacklistService);
@ViewChild(MatPaginator) paginator: MatPaginator;

ngOnInit(): void {

this.fetchData();
this.searchControl.valueChanges.pipe(
    debounceTime(500)
).subscribe(searchText => {
    this.filterData.searchText = searchText;
    this.filterData.page = 1;
    this.fetchData();
});

}
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
}
fetchData(): void {
...
}
onPageChange(event: PageEvent): void {
if (event.pageSize !== this.filterData.pageSize){
this.filterData.page = 1;
} else {
this.filterData.page = event.pageIndex + 1;
}
this.filterData.pageSize = event.pageSize;
this.fetchData();
}
applyFilter(event: any): void {
this.dataSource.filter = event.target.value.trim().toLowerCase();
} i need to filter by statusId and sourceId this is the endpoint im calling from GET_BLACKLIST_LEADS: (filterData?: BlacklistFilterData) => .../blacklist-leads?searchText=${filterData.searchText}&page=${filterData.page}&pageSize=${filterData.pageSize}&statusId=${filterData.statusId}&sourceId=${filterData.sourceId}
its my first time asking help on dc so please say if ive done anything wrong 👍

strong latch
#

Do you have a bug or something, or are you just asking for a design/code review?
If the latter, my main remark would be that you should navigate using the router, to change include the search and page parameters in the URL, when you change the page or submit the search form. If you don't, then navigating away and coming back will make the user lose his previous search and page.

hasty yew
#

yes i have a bug, when i have the statusId and sourceId in filterData everything breaks and it doesnt fetch anything but when i would remove them it works perfectly fine and it fetches correctly so basically when i add them it doesnt fetch anything here ill send the logic for fetchData() but i dont think its the problem unless im being dumb

#

fetchData(): void {
this.blacklistService.getBlacklistLeads(this.filterData).subscribe({
next: (response: any) => {
this.dataSource.data = response;
console.log(response);
if (response && response.length) {
this.length = response.length;
}
},
error: (error: any) => {
console.error('Error fetching blacklist data', error);
},
});
}