1

I have a table and I want to put a search box on top of the table which dynamically search in table data and filter it for enabling user to find table for easier.

my table code is:

<mat-card class="fields-list" *ngIf="tableShown">
      <mat-card-content>
        <mat-card-actions align="end">
          <button type="button" class="topia-btn topia-primary-btn action-buttons" (click)="addClicked()">
            Add New Office
          </button>
        </mat-card-actions>
        <mat-table #table [dataSource]="officePinList">
          <ng-container cdkColumnDef="label">
            <mat-header-cell *cdkHeaderCellDef fxFlex="20%">Label</mat-header-cell>
            <mat-cell *cdkCellDef="let officePin" fxFlex="20%">{{officePin.label}}</mat-cell>
          </ng-container>

          <ng-container cdkColumnDef="postalAddress">
            <mat-header-cell *cdkHeaderCellDef fxFlex="55%">Postal Adress</mat-header-cell>
            <mat-cell *cdkCellDef="let officePin" fxFlex="55%">{{officePin.postalAddress}}</mat-cell>
          </ng-container>

          <ng-container cdkColumnDef="trash-icon">
            <mat-header-cell *cdkHeaderCellDef fxFlex="15%"></mat-header-cell>
            <mat-cell *cdkCellDef="let officePin" fxFlex="15%">
              <mat-icon (click)="deleteGroupOffices(officePin.id)" mat-list-icon matTooltip="Delete" class="icon">
                delete_forever
              </mat-icon>
              <mat-icon (click)="editField(officePin.id)" mat-list-icon matTooltip="Edit" class="icon">edit</mat-icon>
            </mat-cell>
          </ng-container>

          <mat-header-row *cdkHeaderRowDef="displayedColumns"></mat-header-row>
          <mat-row class="table-row" *cdkRowDef="let officePin; columns: displayedColumns;"></mat-row>

        </mat-table>

My table data source is officePinList

How can I filter table data with dynamic search box for the table exactly like this: https://ciphertrick.com/demo/angularajaxsearch/

1 Answer 1

3

Just add an input search box:

Html

<mat-form-field>
  <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>

And afterwards you need to add method that actually adds filter to your source:

Ts

applyFilter(filterValue: string) {
  this.officePinList.filter = filterValue.trim().toLowerCase();
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

9 Comments

ts part is having error. TS2322. type string cannot be assignable to ....
What type is your officePinList source? Is it MatTableDataSource?
officePinList: OfficePin[]; it is a list containing models called OfficePin
try to change it to type officePinList = new MatTableDataSource(data) where data is array of type OfficePin[]
when I change filtervalue string to officePin then trim is giving error as property trim does not exist on type OfficePin ts2339
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.