angular - matSort 不适用于 cdk-virtual-scroll-viewport
问题描述
我使用下面的代码在 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;
}
}
材料分类尚未与虚拟滚动一起使用。如何解决问题?
解决方案
尝试添加一个新的ViewChild
设置器,当它存在时设置排序。
@ViewChild(MatSort)set matSortSetter(value: MatSort) {
if(this.dataSource && value) {
this.dataSource.sort = value;
}
}
推荐阅读
- python - 无法使用 plotly 绘图。(Python 金融食谱)
- html - 防止 CSS Flex 在两个轴上拉伸
- python - 如何在str()中取python条件表达式
- angular - 安装 Angular 的问题
- asp.net - 清除未使用的 iis 网站
- git - 使用 git 合并手动复制目录中的更改
- excel - 没有VBA或Form的Excel单元格中的双列DropList / ComboBox?
- swift - 放大或缩小时如何缩放 SKNode?
- mongodb - Connection to MongoDB ReplicaSet on Kubernetes
- android - Android 模拟器虚拟麦克风在 macOS 上不起作用