【问题标题】:How to add icon to mat-sort-header in mat-table如何将图标添加到 mat-table 中的 mat-sort-header
【发布时间】:2019-06-09 05:42:59
【问题描述】:

我正在使用mat-table,我需要在 mat-sort-header 中添加一个自定义图标,但现在如果我点击图标,表格将被排序,我不希望这种行为,这是代码:

<!-- line Column -->
      <ng-container matColumnDef="line">
        <th mat-header-cell *matHeaderCellDef mat-sort-header disableClear>
          line
          <span *ngIf="lineFiltered == false; else noFilteredline" (click)="filterLine.toggle($event)" class="filter">
           <i class="fa fa-filter" aria-hidden="true"></i></span>
            <ng-template #noFilteredline>
            <span (click)="filterLine.toggle($event)" class="filter success"
              ><i class="fa fa-filter" aria-hidden="true"></i
            ></span>
          </ng-template>
        </th>
        <td mat-cell *matCellDef="let row">{{ row.line }}</td>
      </ng-container>

这是排序标题:

如果我把图标放在外面,它就不会出现。

附言我不能使用 event.stopPropagation()

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    这段代码解决了我的问题:

    <!-- line Column -->
        <ng-container matColumnDef="line">
          <th mat-header-cell *matHeaderCellDef>
              <div class="d-flex">
            <span *ngIf="lineFiltered == false" (click)="filterLine.toggle($event)" class="filter"
              ><i class="fa fa-filter" aria-hidden="true"></i
            ></span>
            <span mat-sort-header disableClear> {{ "contactlens.table.headers.line" | translate }}</span>
            </div>
          </th>
          <td mat-cell *matCellDef="let row">
            {{ row.line }}
    
          </td>
        </ng-container>
    

    【讨论】:

      【解决方案2】:

      我知道我迟到了,但我希望它会拯救某人的一天。我可以通过使用&lt;mat-icon&gt; 标签来做到这一点。

      在 user.component.ts 文件中

      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';
      
      import { UserRoutingModule } from './user-routing';
      import { UserComponent, ConferenceDetailsModal } from './user.component';
      
      import { MatIconModule } from '@angular/material/icon';
      
      
      @NgModule({
        declarations: [
          UserComponent
        ],
        imports: [
          CommonModule,
          UserRoutingModule,
          MatIconModule
        ]
      })
      export class UserModule { }
      
      

      在 user.component.html 文件中

      <table mat-table [dataSource]="dataSource">
          <!-- Title Column -->
          <ng-container matColumnDef="title">
              <th (click)="sortBy('title')" mat-header-cell *matHeaderCellDef mat-sort-header class="Fixed-left">
                  <span class="title-text"> Title
                      <span *ngIf="propertyName === 'title' && !ascending" class="fa fa-arrow-up">
                          <mat-icon>arrow_upward</mat-icon>
                      </span>
                      <span *ngIf="propertyName === 'title' && ascending" class="fa fa-arrow-down">
                          <mat-icon>arrow_downward</mat-icon>
                      </span>
                  </span>
              </th>
              <td mat-cell *matCellDef="let element" [innerHTML]="element.title" class="Fixed-left"> </td>
          </ng-container>
      
          <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
          <tr mat-row
              *matRowDef="let row; columns: displayedColumns | paginate: {id: 'server', itemsPerPage: perPageItem, currentPage: currentPageNumber, totalItems: total}; let i=index;">
          </tr>
      </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-14
        • 2021-11-25
        • 1970-01-01
        • 2018-07-21
        • 2020-06-19
        • 2021-05-20
        • 1970-01-01
        • 2018-11-09
        相关资源
        最近更新 更多