How to add data dynamically to mat-table dataSource?

后端 未结 7 1442
既然无缘
既然无缘 2020-12-08 06:39

I have data streaming from backend and i see it printing in console now i am trying to push event to dataSource its throwing error dataSource is not defined. Ca

相关标签:
7条回答
  • 2020-12-08 07:16

    The following solution worked for me:

    this.socket.on('newMessage', function(event) {
        this.dataSource.data.push(event);
        this.dataSource.data = this.dataSource.data.slice();
    });
    

    Another solution would be calling the _updateChangeSubscription() method for the MatTableDataSource object:

    this.socket.on('newMessage', function(event) {
        this.dataSource.data.push(event);
        this.dataSource._updateChangeSubscription();
    });
    

    This method:

    /** Subscribe to changes that should trigger an update to the table's rendered rows. When the changes occur, process the current state of the filter, sort, and pagination along with the provided base data and send it to the table for rendering. */

    0 讨论(0)
提交回复
热议问题