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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-25 20:22:15 +0000
Seen: 8 times
Last updated: May 25 '23