简体   繁体   English

过滤器在 Angular 使用 FormArray 的材料可编辑表中不起作用

[英]Filter is not working in Angular Material editable table using FormArray

I have created an angular mat-table using FormArray in angular.我使用 angular 中的 FormArray 创建了一个 angular 垫表。 I have also applied filter and pagination.我还应用了过滤器和分页。

But when I search for something it's not working properly.但是当我搜索某些东西时,它无法正常工作。

Can someone please help me to make the filter work?有人可以帮我使过滤器工作吗?

Below is the working demo project link下面是工作演示项目链接

Project Link 项目链接

table-basic-example.ts表基本示例.ts

export class TableBasicExample implements OnInit{
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
 dataSource = new MatTableDataSource<any>();

  VOForm: FormGroup;

  constructor(
    private fb: FormBuilder,
    private _formBuilder: FormBuilder){}

  ngOnInit(): void {
    this.VOForm = this._formBuilder.group({
      VORows: this._formBuilder.array([])
    });

     this.VOForm = this.fb.group({
              VORows: this.fb.array(ELEMENT_DATA.map(val => this.fb.group({
                position: new FormControl(val.position),
                name: new FormControl(val.name),
                weight: new FormControl(val.weight),
                symbol: new FormControl(val.symbol),
                action: new FormControl('existingRecord'),
                isEditable: new FormControl(false),
                isNewRow: new FormControl(false),
              })
              )) //end of fb array
            }); // end of form group cretation

    this.dataSource = new MatTableDataSource((this.VOForm.get('VORows') as FormArray).controls);
    this.dataSource.paginator = this.paginator;

    const filterPredicate = this.dataSource.filterPredicate;
      this.dataSource.filterPredicate = (data: AbstractControl, filter) => {
        return filterPredicate.call(this.dataSource, data.value, filter);
      }
  }

  @ViewChild(MatPaginator) paginator: MatPaginator;

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
  }
  
   applyFilter(event: Event) {
     debugger;
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}

table-basic-example.html表基本示例.html

<div class="mat-elevation-z8">
<mat-form-field>
  <mat-label>Filter</mat-label>
  <input matInput (keyup)="applyFilter($event)" placeholder="Ex. ium" #input>
</mat-form-field>

<form [formGroup]="VOForm" autocomplete="off">
    <ng-container formArrayName="VORows">
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element; let i = index" [formGroup]="element"> 
      {{VOForm.get('VORows').value[i].position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element; let i = index" [formGroup]="element"> 
      <span [hidden]="VOForm.get('VORows').value[i].isEditable">
        {{VOForm.get('VORows').value[i].name}}
      </span>
      <mat-form-field style="width: 50px;"
        [hidden]="!VOForm.get('VORows').value[i].isEditable">
        <input matInput type="text" formControlName="name">
      </mat-form-field>
    </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element; let i = index" [formGroup]="element"> 
      <span [hidden]="VOForm.get('VORows').value[i].isEditable">
        {{VOForm.get('VORows').value[i].weight}}
      </span>
      <mat-form-field style="width: 50px;"
        [hidden]="!VOForm.get('VORows').value[i].isEditable">
        <input matInput type="text" formControlName="weight">
      </mat-form-field>
    </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element; let i = index"[formGroup]="element">
      <span [hidden]="VOForm.get('VORows').value[i].isEditable">
        {{VOForm.get('VORows').value[i].symbol}}
      </span>
      <mat-form-field style="width: 50px;"
        [hidden]="!VOForm.get('VORows').value[i].isEditable">
        <input matInput type="text" formControlName="symbol">
      </mat-form-field>

    </td>
  </ng-container>

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

  <!-- Row shown when there is no matching data. -->
  <tr class="mat-row" *matNoDataRow>
    <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
  </tr>
</table>
                </ng-container>
</form>
  <mat-paginator [pageSizeOptions]="[5, dataSource.data.length>8? dataSource.data.length:''  ]" showFirstLastButtons></mat-paginator>
</div>


Edit-1:编辑-1:

Currently, it showing when you search 'Neon'目前,它会在您搜索“霓虹灯”时显示

在此处输入图像描述

It Should show Like Below:它应该如下所示:

在此处输入图像描述

Thanks in advance.提前致谢。

After a lot of searches, I have created the filter work in the angular mat table.经过大量搜索,我在 angular 垫表中创建了过滤器工作。

Following functionality, I have added into the mat-table with FormArray example:以下功能,我已添加到带有 FormArray 示例的 mat-table 中:

  1. Filter筛选
  2. Go to a specific page. Go 到特定页面。
  3. In place editing into mat table.就地编辑到垫表中。
  4. Add New Row.添加新行。
  5. Edit the existing row.编辑现有行。
  6. Delete the row.删除行。

Here is the link of the project Project-Link-Mat-Table这是项目Project-Link-Mat-Table 的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM