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

前端 未结 23 1424
自闭症患者
自闭症患者 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 02:39

    After reading Material Table not updating post data update #11638 Bug Report I found the best (read, the easiest solution) was as suggested by the final commentor 'shhdharmen' with a suggestion to use an EventEmitter.

    This involves a few simple changes to the generated datasource class

    ie) add a new private variable to your datasource class

    import { EventEmitter } from '@angular/core';
    ...
    private tableDataUpdated = new EventEmitter();
    

    and where I push new data to the internal array (this.data), I emit an event.

    public addRow(row:myRowInterface) {
        this.data.push(row);
        this.tableDataUpdated.emit();
    }
    

    and finally, change the 'dataMutation' array in the 'connect' method - as follows

    const dataMutations = [
        this.tableDataUpdated,
        this.paginator.page,
        this.sort.sortChange
    ];
    

提交回复
热议问题