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

前端 未结 23 1377
自闭症患者
自闭症患者 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:47

    There are two ways to do it because Angular Material is inconsistent, and this is very poorly documented. Angular material table won't update when a new row will arrive. Surprisingly it is told it is because performance issues. But it looks more like a by design issue, they can not change. It should be expected for the table to update when new row occurs. If this behavior should not be enabled by default there should be a switch to switch it off.

    Anyways, we can not change Angular Material. But we can basically use a very poorly documented method to do it:

    One - if you use an array directly as a source:

    call table.renderRows()
    

    where table is ViewChild of the mat-table

    Second - if you use sorting and other features

    table.renderRows() surprisingly won't work. Because mat-table is inconsistent here. You need to use a hack to tell the source changed. You do it with this method:

    this.dataSource.data = yourDataSource;
    

    where dataSource is MatTableDataSource wrapper used for sorting and other features.

提交回复
热议问题