Kunal Vijan
Kunal Vijan

Reputation: 435

How to get the updated count on filter selection?

How can I get the updated total count on filter? As of now I have 6 records when I am filtering for e.g. Gender: Male it shows 1 record but my total still says as "6" and Applicable: 2 Not Applicable: 4 FYI: 0. I wanted to update the same and get the updated results.

table-filter.pipe.ts

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "tableFilter"
})

export class TableFilterPipe implements PipeTransform {
  transform(list: any[], filters: any) {
    const keys = Object.keys(filters).filter(key => filters[key]);
    const filterUser = (user: { [x: string]: any; }) =>
      keys.every(key => {
        if (key == "sdob") {
          return new Date(user["dob"]) >= new Date(filters[key]);
        } else if (key == "edob") {
          return new Date(filters[key]) >= new Date(user["dob"]);
        } else {
          return user[key] === filters[key];
        }
      });
    return keys.length ? list.filter(filterUser) : list;
  }
}

getLatestUser() {
    this.commonService.getAllUser().subscribe(response => {
      this.allUser = response;
      this.totalRecords = this.allUser.length;

      this.getCounts();

      this.allUser.forEach(row => (row.checked = false));
    });
  }

Live View: https://angular-ivy-ww5hgk.stackblitz.io/

Entire Code: https://stackblitz.com/edit/angular-ivy-ww5hgk?file=src%2Fapp%2Fapp.component.ts

Any help would be appreciated.

Upvotes: 2

Views: 709

Answers (1)

meblum
meblum

Reputation: 1944

A bit hacky, but you can do this by moving the pipe to a container, and store the value in a local template variable, then you can use the value in the entire container and bind to the length of it. here is a live example.

Upvotes: 1

Related Questions