I am currently developing a table with pagination that involves performing certain actions based on an array of selected checkboxes. I have referred to the documentation and everything seems to be working fine when it comes to selecting individual rows or all rows;
However, I am facing an issue with the functionality I want to achieve. When my PageSize
is set to "10" and I use the masterToggle
to select all rows, I only want the selection to apply to the 10 rows displayed on the current page, not the entire dataset which contains around 300 records.
Is there a way to modify the behavior of the masterToggle
so that it selects only the rows visible based on the PageSize setting? For example, if I switch the PageSize
to 20, only the first 10 rows should remain selected.
Below is the relevant section of my code:
/** Checks if all elements are selected. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
/** Selects all rows if they are not all selected; clears selection otherwise. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}