3
votes

I have a mat-table using the code below with sortable headers. However I also want to put a menu in the header for custom filtering. The problem is that because the whole header clickable and changes the 'sort' of the column, when i click on the menu it also sorts because the menu button is within the header.

<mat-table #table [dataSource]="dataSource" matSort>
                <ng-container matColumnDef="Borrower1">
                  <mat-header-cell *matHeaderCellDef mat-sort-header> 
                    <div class="header">
                      Borrower1
                      <button mat-button [matMenuTriggerFor]="menu" #matMenuTrigger="matMenuTrigger" >
                        <mat-icon>filter_list</mat-icon>
                      </button>
                    </div>
                     <mat-menu #menu="matMenu"   >
                          <div (mouseleave)="matMenuTrigger.closeMenu()">
                            <button mat-menu-item>Item 1</button>
                            <button mat-menu-item>Item 2</button>
                            <button mat-menu-item>Item 3</button>
                            <button mat-menu-item>Item 4</button>
                            <button mat-menu-item>Item 5</button>
                          </div>
                        </mat-menu>     
                  </mat-header-cell>

                  <mat-cell *matCellDef="let element"> {{element.Borrower1}} </mat-cell>

                </ng-container>
                <ng-container matColumnDef="_id">
                  <mat-header-cell *matHeaderCellDef mat-sort-header> 
                    _id
                  </mat-header-cell>
                  <mat-cell *matCellDef="let element"> {{element._id}} </mat-cell>
                </ng-container>
                <ng-container matColumnDef="edit">
                  <mat-header-cell *matHeaderCellDef></mat-header-cell>
                  <mat-cell *matCellDef="let element">
                    <a (click)="editDialog(element._id)" type="button">
                            <mat-icon class="icon">edit</mat-icon>
                    </a>
                  </mat-cell>
                </ng-container>
                <ng-container matColumnDef="delete">
                  <mat-header-cell *matHeaderCellDef></mat-header-cell>
                  <mat-cell *matCellDef="let element">
                    <a (click)="deletePost(element._id)" type="button">
                            <mat-icon class="icon">delete</mat-icon>
                    </a>
                  </mat-cell>
                </ng-container>   
                <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
                <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
              </mat-table>

Is there a way to get around this

3

3 Answers

2
votes

Try taking everything in the header that you don't want triggering a sort, wrap it in a div, and put a click handler on it that calls $event.stopPropagation().

0
votes

Simply remove mat-sort-header directive from header you don't want to be clickable (SORTABLE):

     <mat-table #table [dataSource]="dataSource" matSort>
        <ng-container matColumnDef="_id">
            <mat-header-cell *matHeaderCellDef>
                Id // NOT SORTABLE
            </mat-header-cell>
            <mat-cell *matCellDef="let row"> {{row.Id}} </mat-cell>
        </ng-container>
        <ng-container matColumnDef="edit">
            <mat-header-cell *matHeaderCellDef mat-sort-header>
                Number // SORTABLE
            </mat-header-cell>
            <mat-cell *matCellDef="let row">{{row.Number}}</mat-cell>
        </ng-container>
    </mat-table>
0
votes

Put your menu inside a div and add (click)="$event.stopPropagation()" to your div like

<div (click)="$event.stopPropagation()">
   your menu....
</div>

Hope it solve your issue.