How to select all items in an Angular Mat-table on the current page after sorting?
add a comment
To select all items in an Angular Mat-table on the current page after sorting, you can use the following steps:
@ViewChild(MatTable) table: MatTable<any>;
selectPageItems() {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
const endIndex = startIndex + this.paginator.pageSize;
const pageItems = this.dataSource.data.slice(startIndex, endIndex);
for (let item of pageItems) {
this.selection.select(item);
}
}
this.dataSource.sort = this.sort;
this.dataSource.sortingDataAccessor = (data, sortHeaderId) => {
return data[sortHeaderId].toLocaleLowerCase();
};
this.table.renderRows();
this.selectPageItems();
This will select all the items on the current page of the Mat-table after sorting.
Asked: 2023-05-25 20:22:15 +0000
Seen: 8 times
Last updated: May 25 '23