Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

angular - Material table sorting when sort header IDs (which defaults to the column name) does not match the data's properties

.ts

this.displayedColumns = [
      { key: 'id', header: '#' },
      { key: 'fullname', header: 'Full name' },
      { key: 'email', header: 'email' },
      { key: 'roleName', header: 'Role' }
];
this.displayedColumnsKeys = this.displayedColumns.map(col => col.key);

.html

<table mat-table [dataSource]="dataSource" multiTemplateDataRows matSort class="mat-elevation-z8">

  <ng-container *ngFor="let dispCol of displayedColumns; let colIndex = index" matColumnDef="{{dispCol.key}}">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{dispCol.header}} </th>
    <td mat-cell *matCellDef="let element"> {{element[dispCol.key]}} </td>
  </ng-container>

  <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
  <ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
      <div class="example-element-detail"
           [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
        <div class="example-element-diagram">
          <div class="example-element-position"> {{element.position}} </div>
          <div class="example-element-symbol"> {{element.symbol}} </div>
          <div class="example-element-name"> {{element.name}} </div>
          <div class="example-element-weight"> {{element.weight}} </div>
        </div>
        <div class="example-element-description">
          {{element.description}}
          <span class="example-element-description-attribution"> -- Wikipedia </span>
        </div>
      </div>
    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumnsKeys"></tr>
  <tr mat-row *matRowDef="let element; columns: displayedColumnsKeys;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? null : element">
  </tr>
  <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
  <tr class="mat-row" *matNoDataRow>
    <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
  </tr>
</table>

Sorting is not working because my header ids don't match the properties on the array of objects i'm getting from my backend.

What I understand so far is I have to make a custom sortingDataAccessor but have no idea how to achieve that. Thanks in advance for the help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

@ViewChild(MatSort) sort: MatSort;

ngOnInit() {
  this.dataSource = new MatTableDataSource(yourData);
  this.dataSource.sortingDataAccessor = (row, key) => {
    switch(key) {
      case 'columnName': return row.serverName;
      default: return row[key];
    }
  };
  this.dataSource.sort = sort;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...