'How to add paginator and sorting to unknown number of angular material tables?

I have no problem in adding sorting and pagination to predefined tables like this:

@ViewChild('myTableSort') myTableSort:MatSort;
@ViewChild('myTablePaginator') myTablePaginator:MatPaginator;

then after getting table data:

this.tableData = new MatTableDataSource(data);
this.tableData.sort = this.myTableSort;
this.tableData.paginator = this.myTablePaginator;

This works fine but when I have an unknown number of tables, I need to programmatically add a paginator and sort to each table.

Can I somehow declare a new paginator myTablePaginator2 after getting table data?

How can I add a paginator and sort dynamically after creating tables?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source