Angular + Material - How to refresh a data source (mat-table)

前端 未结 23 1353
自闭症患者
自闭症患者 2020-11-28 01:59

I am using a mat-table to list the content of the users chosen languages. They can also add new languages using dialog panel. After they added a language and returned back.

23条回答
  •  感情败类
    2020-11-28 03:05

    In Angular 9, the secret is this.dataSource.data = this.dataSource.data;

    Example:

    import { MatTableDataSource } from '@angular/material/table';
    
    dataSource: MatTableDataSource;
    
    refresh(): void {
        this.applySomeModif();
        // Do what you want with dataSource
    
        this.dataSource.data = this.dataSource.data;
    }
    
    applySomeModif(): void {
        // add some data
        this.dataSource.data.push(new MyObject());
        // delete index number 4
        this.dataSource.data.splice(4, 0);
    }
    

提交回复
热议问题