matSort不适用于cdk-virtual-scroll-viewport

时间:2019-11-14 19:53:56

标签: angular angular-material angular-cdk

在Angular应用程序中使用cdk-virtual-scroll-viewport滚动时,我使用了以下代码在表中呈现数据。物料分类功能也已使用matSort实现。请参考下面的代码。

table.component.html:            

    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Symbol </th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <ng-template let-row matRowDef cdkVirtualFor [matRowDefColumns]="displayedColumns" [cdkVirtualForOf]="rows">
      <tr mat-row></tr>
    </ng-template>
  </table>
</cdk-virtual-scroll-viewport>

table.component.ts:

import { Component, OnInit, ViewChild, Inject } from '@angular/core';
import { VIRTUAL_SCROLL_STRATEGY } from "@angular/cdk/scrolling";
import { Observable, of, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import {MatSort, MatTableDataSource} from '@angular/material';
import { TableVirtualScrollStrategy } from './table-vs-strategy.service';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
  {position: 11, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 12, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 13, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 14, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 15, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 16, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 17, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 18, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 19, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 20, name: 'Neon', weight: 20.1797, symbol: 'Ne'}
];
@Component({
  selector: 'app-table',
  templateUrl: 'table.component.html',
  providers: [{
    provide: VIRTUAL_SCROLL_STRATEGY,
    useClass: TableVirtualScrollStrategy,
  }],
})
export class TableComponent implements OnInit {

  // Manually set the amount of buffer and the height of the table elements
  static BUFFER_SIZE = 3;
  rowHeight = 48;
  headerHeight = 56;

  rows: Observable<Array<any>> = of(ELEMENT_DATA);

  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];

  dataSource = new MatTableDataSource(ELEMENT_DATA);

  gridHeight = 400;
@ViewChild(MatSort) sort: MatSort;

  constructor(@Inject(VIRTUAL_SCROLL_STRATEGY) private readonly scrollStrategy: TableVirtualScrollStrategy) {}

  public ngOnInit() {
    const range = Math.ceil(this.gridHeight / this.rowHeight) + TableComponent.BUFFER_SIZE;
    this.scrollStrategy.setScrollHeight(this.rowHeight, this.headerHeight);

    this.dataSource = combineLatest([this.rows, this.scrollStrategy.scrolledIndexChange]).pipe(
      map((value: any) => {

        // Determine the start and end rendered range
        const start = Math.max(0, value[1] - TableComponent.BUFFER_SIZE);
        const end = Math.min(value[0].length, value[1] + range);

        // Update the datasource for the rendered range of data
        return value[0].slice(start, end);
      })

    );
    this.dataSource.sort = this.sort;
  }
}

材料排序不适用于虚拟滚动。该如何解决?

2 个答案:

答案 0 :(得分:1)

尝试添加一个新的ViewChild设置器,该设置器在存在排序器时对其进行设置。

@ViewChild(MatSort)set matSortSetter(value: MatSort) {
  if(this.dataSource && value) {
    this.dataSource.sort = value;
  }
}

答案 1 :(得分:1)

实际上,您在这里有两个选择:

  • 处理(matSortChange)事件,例如Angular Material文档的the first example

  • 因为MatSort指令已经公开了可观察的变量来处理更改,所以您可以像the second example中那样访问MatSort实例并处理数据源中MatSort的事件:

component.ts

import { MatSort } from '@angular/material';

@ViewChild(MatSort, {static: true}) sort: MatSort;

如果您要使用MatTableDataSource(再次类似于doc中的第二个示例),则只应在该处传递该实例:

dataSource = new MatTableDataSource(ELEMENT_DATA);

@ViewChild(MatSort, {static: true}) sort: MatSort;

ngOnInit() {
  this.dataSource.sort = this.sort;
}

然后MatTableDataSource将处理all the magic under the hood

但是由于您使用的是Observable,因此您需要自己处理排序更改,例如:

public ngOnInit() {
  ...    
  const sortChange = merge(this.sort.sortChange, this.sort.initialized);

  this.dataSource = combineLatest([this.rows, this.scrollStrategy.scrolledIndexChange, sortChange]).pipe(
    map(([rows, index, sort]: [any[], number, Sort|void]) => {

      if (sort) {
        rows = this.sortData(rows.slice(), sort);
      }

      // Determine the start and end rendered range
      const start = Math.max(0, index - AppComponent.BUFFER_SIZE);
      const end = Math.min(rows.length, index + range);

      // Update the datasource for the rendered range of data
      return rows.slice(start, end);
    })
  );

}

  /**
 * Your implementation here
 */
sortData(data: any[], sort: Sort) {
  console.log(sort)
  return data.sort((a, b) => {
    return sort.direction === 'asc' ? a[sort.active] - b[sort.active] : b[sort.active] - a[sort.active]
  });
}

Stackblitz Example

要保持一致,您可以创建自己的自定义数据源,甚至可以重用已经处理所有情况的MatTableDataSource

例如,以下是如何重用MatTableDataSource的想法:

this.subscription = combineLatest([this.rows, this.scrollStrategy.scrolledIndexChange]).pipe(
  tap(([rows, index]: [any[], number]) => {
    // Determine the start and end rendered range
    const start = Math.max(0, index - AppComponent.BUFFER_SIZE);
    const end = Math.min(rows.length, index + range);

    // Update the datasource for the rendered range of data
    const data = rows.slice(start, end);

    this.dataSource = new MatTableDataSource(data);
    this.dataSource.sort = this.sort;
  })
).subscribe();

Stackblitz Example