Jerome
Jerome

Reputation: 874

filterPredicate only seems to run when the filter variable is truthy. How do I get it to run everytime?

Some of the data is going to be taken out based on some logic that's unrelated to the filter input string. But I still need the filterPredicate function to run every time filter is changed.

Most of the time this works but if the filter variable is falsey the filterPredicate doesn't run at all and every piece of data is displayed. Which is something that I don't want.

streamingClients: MatTableDataSource<any> = new MatTableDataSource();


ngOnInit() {
    this.eventsService.getClients("", QueryType.AllStreamClients).subscribe({
      next: (response: any) => {
        this.streamingClients = new MatTableDataSource(response.result);

        this.streamingClients.paginator = this.matPaginator;

        this.streamingClients.filterPredicate = (data: any, filter: string): boolean => {
          const filterValue = filter.trim().toLowerCase();
        
          const columnValue = data[this.selectedColumn]?.toString().toLowerCase();
          const matchesFilter = columnValue?.includes(filterValue);

          // if statement that I need to execute even if filter = ""
          if (data.staff && !this.selectedClientTypes.includes("STAFF")) {
              return false
          }

          return matchesFilter;
        };
      },
      error: (error) => {
        console.error('Error loading clients:', error);
        this.isLoading = false;
      }
    });
  }

Upvotes: 1

Views: 25

Answers (0)

Related Questions