Reputation: 161
I'm trying to unbind the row selection from the table rows so that clicking anywhere within the row doesn't tick the checkbox. I want the checkbox only to be ticked when the box itself is clicked, so that I can make room to add expandable rows when the user clicks elsewhere (kind of like the Gmail inbox).
I just can't figure out how to unbind the checkbox ticking from the rows.
Here is the markup for the checkbox column
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
(click)="$event.stopPropagation()"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()"
[aria-label]="checkboxLabel()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"
[aria-label]="checkboxLabel(row)">
</mat-checkbox>
</td>
</ng-container>
Upvotes: 5
Views: 3059
Reputation: 8650
I don't see the relevant template code in your question but it looks like you are following the example given in the material docs. In which case, in the mat-table
, at the bottom, there will be the following code
<tr mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</tr>
Simply remove the click
event which toggles the row
to selected
<tr mat-row *matRowDef="let row; columns: displayedColumns;">
</tr>
Now your checkbox will not be checked on click of the row.
Note: Doing this will also mean using
$event.stopPropagation()
is no longer needed so you can go ahead and remove that from your template as well.
Upvotes: 6