【问题标题】:Default sorting in Angular Material with nested objects具有嵌套对象的 Angular Material 中的默认排序
【发布时间】:2020-02-07 20:54:48
【问题描述】:

我使用 mat 表进行排序,我想在某个列上设置默认排序。

这适用于普通属性,但不适用于嵌套属性。

这是我的桌子

<table mat-table [dataSource]="dataSource" multiTemplateDataRows 
       matSort matSortActive="break" matSortDirection="asc"
       class="mat-elevation-z4 w-100">

我的嵌套排序

this.dataSource.sort = this.sort;
   this.dataSource.sortingDataAccessor = (item, property) => {
   switch(property) {
      case 'break': return item.break.start;
      default: return item[property];
   }
};

这种嵌套排序也很好,当您手动单击标题进行排序时,它会按照我的预期进行,但默认情况下它不会排序,只显示排序箭头。

这就是页面加载时的样子:

这里是对应的stackblitz

【问题讨论】:

  • this may help 或者如果问题仍然存在,您可以添加 stackblitz 演示
  • 很遗憾,此解决方案不适用于嵌套对象。

标签: angular angular-material


【解决方案1】:

我在 5 月份研究时,初始排序无法正常工作。

就我而言,有必要编写自己的setSortHeader 函数,该函数在获取数据后执行。

setSortHeader() {
  this.sort.active = 'break';
  this.sort.direction = 'desc';
  this.sort.sortChange.emit({ active: this.sort.active, direction: this.sort.direction });

  const sortHeader = this.sort.sortables.get('break');
  if (sortHeader) sortHeader['_setAnimationTransitionState']({ toState: 'active' });
}

我不确定这段代码是否仍然必要,但在我的情况下是有效的。

【讨论】:

    【解决方案2】:
    <ng-container matColumnDef="firstName">
        <th mat-header-cell class="mat-header-sticky" id="firstName" *matHeaderCellDef mat-sort-header tabindex="0"
          [attr.aria-label]="'First Name Tool Tip'" [matTooltip]="tooltipConfiguration['firstName']"
          [matTooltipPosition]="'above'" [matTooltipClass]="'matTooltip-panel'">
          First Name
        </th>
        <td mat-cell *matCellDef="let contact" class="align-center">
          <span class="display-block overflow-ellipsis">
            {{ contact.firstName }}
          </span>
        </td>
    </ng-container>
    

    只需使用相同的matColumnDef="firstName"{{ contact.firstName }},注意.firstName 属性与matColumnDef ID 匹配。这是根据角度文档。

    【讨论】:

      【解决方案3】:

      请尝试以下。


      如果要更改默认方向,请在 ngOnInit 方法调用 this.sortItem 时更改它

      // app.component.ts

      import { Component, ViewChild } from '@angular/core';
      
      import { Sort, MatSort, MatPaginator } from '@angular/material';
      
      import { interval } from 'rxjs';
      
      @Component({
        selector: 'my-app',
        templateUrl: './app.component.html',
        styleUrls: [ './app.component.css' ]
      })
      export class AppComponent  {
        public displayedColumns: string[] = ['name', 'break',];
      
        public sortedData: any;
      
        constructor( ) { }
      
        ngOnInit() {
          setTimeout(() => {         
            this.sortItem({ active: 'break', direction: 'asc' });
          }, 1000);
        }
        compare(a: number | string, b: number | string, isAsc: boolean) {
          return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
        }
      
        sortItem(sort: Sort) {
          const data = DATA_SOURCE.slice();
          if (!sort.active || sort.direction === '') {
            this.sortedData = data;
            return;
          }
      
          this.sortedData = data.sort((a, b) => {
            const isAsc = sort.direction === 'asc';
            switch (sort.active) {
              case 'break': return this.compare(a.breakTime.start, b.breakTime.start, isAsc);        
              default: return 0;
            }
          });
        }
      }
      
      const DATA_SOURCE = [
        {
          name: 'Alice',
          breakTime: {
            start: '14-00',
            end: '14-00'
          },
        },
        {
          name: 'Steve',
          breakTime: {
            start: '10-00',
            end: '11-00'
          },
        },
        {
          name: 'Bob',
          breakTime: {
            start: '12-00',
            end: '13-00'
          },
        },
      ];
      

      // app.component.html

      <table mat-table [dataSource]="sortedData" multiTemplateDataRows 
             matSort (matSortChange)="sortItem($event)" 
             class="mat-elevation-z4 w-100">
      
        <ng-container matColumnDef="name">
          <th mat-header-cell *matHeaderCellDef> Name </th>
          <td mat-cell *matCellDef="let item"> {{item.name}} </td>
        </ng-container>
      
        <ng-container matColumnDef="break">
          <th mat-header-cell *matHeaderCellDef mat-sort-header > Break </th>
          <td mat-cell *matCellDef="let element">
            {{element.breakTime.start}} - {{element.breakTime.end}}
          </td>
        </ng-container>
      
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let element; columns: displayedColumns;"
            class="element-row"
            [class.example-expanded-row]="expandedElement === element"
            (click)="expandedElement = expandedElement === element ? null : element">
        </tr>
      </table>
      

      【讨论】:

        猜你喜欢
        • 2018-07-31
        • 2018-03-26
        • 1970-01-01
        • 2021-02-10
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        相关资源
        最近更新 更多