2

I'm trying to build a table with filtering by letters. So if I click on 'A', only the data beginning with 'A' should be displayed in the table. How do I implement that?

I already set up a table with hard coded data. This is how it should look like:

Table Filtering by letter

So if I click on 'D', the 'Dondini' row should only be displayed in that case.

Following some code snippets of my table:

HTML:

    <div class="mat-elevation-z8">
      <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">

...

        <!-- Name Column -->
        <ng-container matColumnDef="name">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
          <td mat-cell *matCellDef="let element"> {{element.name}} </td>
        </ng-container>

...

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
      </table>

      <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
    </div>

TS:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

...

export class OpereBuffeComponent implements OnInit {
  displayedColumns: string[] = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);

  @ViewChild(MatSort, {static: true}) sort: MatSort;
  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

  ngOnInit() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
  }
}
Edric
  • 24,639
  • 13
  • 81
  • 91
  • you could use a pipe or filter the array like so array.filter(element => element.name.charAt(0) === filterCharacter); Receive the filter character from the clickevent on the character – sagat Aug 05 '19 at 12:10
  • check this answer https://stackoverflow.com/questions/51515695/filtering-different-columns-in-a-material-table and the live demo – Muhammed Albarmavi Aug 05 '19 at 12:25

2 Answers2

0

Do it like this:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

...

export class OpereBuffeComponent implements OnInit {
  displayedColumns: string[] = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);

  @ViewChild(MatSort, {static: true}) sort: MatSort;
  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

  ngOnInit() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
  }

  receiveFilterChar(c: string) {
   this.dataSource.data = this.ELEMENT_DATA.filter(element => element.name.trim().toLowerCase().charAt(0) === c);
  }
}

You can just filter the initial array and set the filtered result to the datasource. If the view doesnt update u can use the ChangeDetectorRef and detect UI changes.

regards

sagat
  • 1,351
  • 12
  • 15
0

You can do this by overriding filterPredicate of MatTableDataSource as follows.

TS

letters: string[] = ['H', 'L', 'B', 'C', 'N'];

ngOnInit() {
    ...
    this.dataSource.filterPredicate = (data: Element, filter: string) => data['name'][0].toLocaleLowerCase() === filter[0];
}

applyFilter(filterValue: string): void {

this.dataSource.filter = filterValue.trim().toLocaleLowerCase();
}

HTML

   <div>
    <input *ngFor="let b of letters" type="button" [value]="b" (click)="applyFilter(b)">
  </div>

Find working StackBlitz

Sudarshana Dayananda
  • 5,165
  • 2
  • 23
  • 45