【问题标题】:Need to reassign data, paginator, and sort on every new row in mat-table需要对 mat-table 中的每一行重新分配数据、分页器和排序
【发布时间】:2020-11-12 04:11:11
【问题描述】:

我有一个<mat-table>,在那里我实现了一个addRow() 函数,它使用一组基于数据类型的默认值在表中创建一个新行。

我遇到的问题是,每次创建新行时,我都需要重新分配 MatTableDataSourceMatPaginatorMatSort。如果我不执行重新分配,则不会显示新数据,或者分页/排序不起作用。

所有这些重新分配似乎代价高昂且迂回曲折,尤其是在较大的数据集上,但我不完全确定如何做得更好。

dynamic-table.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { TableField } from 'src/app/models/table-field';

const fields: TableField[] = [
  { name: "job", type: "string", editType: "free", options: [] },
  { name: "wage", type: "string", editType: "free", options: [] },
  { name: "state", type: "string", editType: "spinner", options: ["MI", "CA", "TX"] },
  { name: "isUnion", type: "boolean", editType: "checkbox", options: [] }
];

const rows: any[] = [
  { job: "J1", wage: "10.00", state: "MI", isUnion: false },
  { job: "J2", wage: "15.00", state: "TX", isUnion: true },
  { job: "J3", wage: "77.00", state: "CA", isUnion: true }
];

@Component({
  selector: 'app-dynamic-table',
  templateUrl: './dynamic-table.component.html',
  styleUrls: ['./dynamic-table.component.css']
})
export class DynamicTableComponent implements OnInit {
  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  @ViewChild(MatSort, { static: true }) sort: MatSort;

  Fields: {}[];
  Rows: {}[];
  ColumnHeaders: string[];
  dataSource: MatTableDataSource<{}>;
  constructor() { }

  ngOnInit() {
    this.Fields = fields.map(field => field);
    this.ColumnHeaders = this.Fields.map(field => field["name"]);
    this.Rows = rows.map(row => row);
    console.log(this.Fields);
    console.log(this.Rows);
    console.log(this.ColumnHeaders);
    this.dataSource = new MatTableDataSource(rows);
  }

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

  addRow() {
    let row = {};
    fields.forEach(field => {
      switch(field.editType) { 
        case "free": { 
           row[field.name] = "default"
           break; 
        } 
        case "spinner": { 
           row[field.name] = field.options[0]; 
           break; 
        }
        case "checkbox": { 
          row[field.name] = false;
          break; 
       }
      } 
    });
    this.Rows = this.Rows.concat(row);
    this.dataSource = new MatTableDataSource(this.Rows);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  printRows() {
    console.log(this.dataSource.data)
  }
}

dynamic-table.component.html

<table mat-table [dataSource]="dataSource" matSort>
    <ng-container *ngFor="let field of Fields" matColumnDef="{{field.name}}">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>{{field.name}}</th>
        <td mat-cell *matCellDef="let row">
            <input *ngIf="field.editType == 'free'" matInput [(ngModel)]="row[field.name]" placeholder="{{row[field.name]}}" required>
            <mat-select *ngIf="field.editType == 'spinner'" [(ngModel)]="row[field.name]">
                <mat-option *ngFor="let option of field.options" [value]="option">{{option}}</mat-option>
            </mat-select>
            <mat-checkbox *ngIf="field.editType == 'checkbox'" [(ngModel)]="row[field.name]">
            </mat-checkbox>
        </td>
    </ng-container>

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

<mat-paginator #paginator [pageSizeOptions]="[10, 20, 30]" showFirstLastButtons></mat-paginator>
<button mat-button color="primary" (click)="addRow()">Add Row</button>
<button mat-button color="primary" (click)="printRows()">Print Rows</button>

【问题讨论】:

    标签: angular typescript angular-material mat-table


    【解决方案1】:

    您可以使用 setter 进行分页和排序组件

    @ViewChild(MatPaginator, { static: false })
      set paginator(value: MatPaginator) {
        if(this.dataSource) {
          this.dataSource.paginator = value;
        }
      }
      @ViewChild(MatSort, { static: false }) 
      set sort(value: MatSort) {
        if(this.dataSource) {
          this.dataSource.sort = value;
        }
      }
    

    您可以在此处删除分配:

      ngAfterViewInit() {
        //this.dataSource.paginator = this.paginator;
        //this.dataSource.sort = this.sort;
      }
    

    要修改表,您别无选择,只能重新分配数据源。 当您将添加新行时,只需修改数据源。感谢 setter,分页器和排序组件将被更新。

      addRow() {
        let row = {};
        fields.forEach(field => {
          switch(field.editType) { 
            case "free": { 
               row[field.name] = "default"
               break; 
            } 
            case "spinner": { 
               row[field.name] = field.options[0]; 
               break; 
            }
            case "checkbox": { 
              row[field.name] = false;
              break; 
           }
          } 
        });
        this.Rows = this.Rows.concat(row);
        this.dataSource.data = this.Rows;
      }
    

    【讨论】:

    • 作为 TS/JS 的新手,是否通过引用分配给 MatTableDataSource?我希望这个表在我们上线后能够容纳数千行,因此重新分配每个新行上的所有数据听起来像是一个潜在的问题。
    • 例如,您可以使用 15,000 个条目进行测试,不会有任何性能问题。静态数据的 DataSource 的重新分配在 Angular Material 的官方文档中有描述。但是如果你使用外部数据源,我建议你使用 MatDataSource 的 connect() 方法,这里的大纲中有说明:medium.com/@jchavez1894/…
    • 感谢您提供附加链接。静态数据仅用于 POC,我将更新以从实时/外部源中提取。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2021-04-04
    • 2019-08-15
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多