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

前端 未结 23 1346
自闭症患者
自闭症患者 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条回答
  •  -上瘾入骨i
    2020-11-28 02:39

    Best way to do this is by adding an additional observable to your Datasource implementation.

    In the connect method you should already be using Observable.merge to subscribe to an array of observables that include the paginator.page, sort.sortChange, etc. You can add a new subject to this and call next on it when you need to cause a refresh.

    something like this:

    export class LanguageDataSource extends DataSource {
    
        recordChange$ = new Subject();
    
        constructor(private languages) {
          super();
        }
    
        connect(): Observable {
    
          const changes = [
            this.recordChange$
          ];
    
          return Observable.merge(...changes)
            .switchMap(() => return Observable.of(this.languages));
        }
    
        disconnect() {
          // No-op
        }
    }
    

    And then you can call recordChange$.next() to initiate a refresh.

    Naturally I would wrap the call in a refresh() method and call it off of the datasource instance w/in the component, and other proper techniques.

提交回复
热议问题