【问题标题】:How to implement Filter in Material table in Angular?如何在 Angular 的材料表中实现过滤器?
【发布时间】:2022-02-09 20:05:12
【问题描述】:

.HTML:

如何在这个组件中实现过滤器,因为我已经用扩展表实现了它,但是我的过滤器不起作用。

    <mat-dialog-content style="width: 63rem; overflow: initial;">       
      <div class="searchField">
        <mat-form-field appearance="standard">
          <mat-label>Filter</mat-label>
          <input matInput (keyup)="applyFilter($event)" placeholder="Ex. Tele" #input>
        </mat-form-field>
      </div>  
     
    </mat-dialog-content>

.TS:

    getProductList() {
        this.commonservice.getProdList().subscribe((response: any) => {
          console.log('Response from API is ', response);
          if (response) {
            this.productInfo = response;
            this.dataSource = this.productInfo;
            console.log('Product Info ', this.productInfo);
          }
        }, (error) => {
          console.log('Error is ', error);
        });
      }

    applyFilter(event: Event) {
        const filterValue = (event.target as HTMLInputElement).value;
        this.dataSource.filter = filterValue.trim().toLocaleLowerCase();
        console.log('filtered data ', this.dataSource.filter);
      }

【问题讨论】:

    标签: angular typescript angular-material mat-table


    【解决方案1】:

    假设 dataSource 是一个数组

    this.dataSource = this.dataSource.filter((d)=>d === filterValue.trim().toLocaleLowerCase());
    

    【讨论】:

    • 仍然无法过滤数据。
    • 您的数据源是一个对象数组。你需要做“d.somePropertyOfObjectInArray === filterValue.trim().toLocaleLowerCase()”
    • 我用的是这个:this.dataSource = this.dataSource.filter((d:any) => d.product_name === filterValue.trim().toLocaleLowerCase());
    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多