5

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>

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I was verbatim following the example. And your solution worked. Very simple, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.