Angular- Material table 排序不正确
Angular- Material table sorting not working in proper way
我正在创建一个 table,它应该按文件名和日期排序 我为此目的遵循了 Angular Material 文档,但没有工作,它没有显示编译器或浏览器控制台,table 看起来像这样:
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="File">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="Date">
<mat-header-cell *matHeaderCellDef mat-sort-header>Date </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Date"> {{element.date}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="Extension">
<mat-header-cell *matHeaderCellDef mat-sort-header>Extension</mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Extension"> {{element.extension}} </mat-cell>
</ng-container>
<ng-container matColumnDef="View">
<mat-header-cell *matHeaderCellDef> Actions </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="View"> <a href="{{element.url}}" target="_blank" class="green-text"><i class="material-icons">visibility</i></a></mat-cell>
</ng-container>
<ng-container matColumnDef="Delete">
<mat-header-cell *matHeaderCellDef> Delete </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Delete"> <a class="red-text"><i class="material-icons">delete_forever</i></a></mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
这是我的 ts 代码
export class FilesComponent implements OnInit {
displayedColumns: string[] = ['File', 'Extension', 'Date', 'View', 'Delete'];
dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
data = ELEMENT_DATA;
selects: any;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatSort, {static: true}) sort: MatSort;
constructor(private addFile: MatDialog) {
}
ngOnInit(): void {
this.Materialize();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
Materialize() {
this.selects = document.querySelectorAll('select');
M.FormSelect.init(this.selects);
}
changeFiles(value) {
console.log('option:', value);
switch (parseInt(value)) {
case 1:
this.dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
break;
case 2:
this.dataSource = new MatTableDataSource<Files>(Charts);
break;
case 3:
this.dataSource = new MatTableDataSource<Files>(Documents);
break;
default:
this.dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
break;
}
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
}
事件排序 header 已应用并且样式(排序箭头)出现在 table 数据未排序。
matColumnDef 名称和*matCellDef 实际值名称应该相同
在component.html
改变
<ng-container matColumnDef="File">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell></ng-container>
到
<ng-container matColumnDef="fileName">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell>
</ng-container>
在 ts:
改变
displayedColumns: string[] = ['File', 'Extension', 'Date', 'View', 'Delete'];
到
displayedColumns: string[] = ['fileName', 'Extension', 'Date', 'View', 'Delete'];
@user12129132 的答案将适用于正常排序(您需要更改数据源中的字段值以匹配列定义中的字段值,并在 matColumn 定义中使用相同的字段值)但是您的文件名中包含数字,它不会被排序 properly.For 我们可以使用 sortingDataAccessor 的目的。
在此处查看工作版本 - Stackblitz:https://stackblitz.com/edit/angular-g4svyj
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.sortingDataAccessor = (data, attribute) => {
let formattedData=data[attribute]
if(attribute=="fileName")
{
formattedData=parseInt(formattedData.match(/(\d)*$/)[0]);
}
return formattedData;
};
this.dataSource.paginator = this.paginator;
}
您需要将 MatSortModule
添加到 app.module.ts
中的导入数组
我正在创建一个 table,它应该按文件名和日期排序 我为此目的遵循了 Angular Material 文档,但没有工作,它没有显示编译器或浏览器控制台,table 看起来像这样:
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="File">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="Date">
<mat-header-cell *matHeaderCellDef mat-sort-header>Date </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Date"> {{element.date}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="Extension">
<mat-header-cell *matHeaderCellDef mat-sort-header>Extension</mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Extension"> {{element.extension}} </mat-cell>
</ng-container>
<ng-container matColumnDef="View">
<mat-header-cell *matHeaderCellDef> Actions </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="View"> <a href="{{element.url}}" target="_blank" class="green-text"><i class="material-icons">visibility</i></a></mat-cell>
</ng-container>
<ng-container matColumnDef="Delete">
<mat-header-cell *matHeaderCellDef> Delete </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="Delete"> <a class="red-text"><i class="material-icons">delete_forever</i></a></mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
这是我的 ts 代码
export class FilesComponent implements OnInit {
displayedColumns: string[] = ['File', 'Extension', 'Date', 'View', 'Delete'];
dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
data = ELEMENT_DATA;
selects: any;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
@ViewChild(MatSort, {static: true}) sort: MatSort;
constructor(private addFile: MatDialog) {
}
ngOnInit(): void {
this.Materialize();
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
Materialize() {
this.selects = document.querySelectorAll('select');
M.FormSelect.init(this.selects);
}
changeFiles(value) {
console.log('option:', value);
switch (parseInt(value)) {
case 1:
this.dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
break;
case 2:
this.dataSource = new MatTableDataSource<Files>(Charts);
break;
case 3:
this.dataSource = new MatTableDataSource<Files>(Documents);
break;
default:
this.dataSource = new MatTableDataSource<Files>(ELEMENT_DATA);
break;
}
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
}
事件排序 header 已应用并且样式(排序箭头)出现在 table 数据未排序。
matColumnDef 名称和*matCellDef 实际值名称应该相同
在component.html
改变
<ng-container matColumnDef="File">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell></ng-container>
到
<ng-container matColumnDef="fileName">
<mat-header-cell *matHeaderCellDef mat-sort-header> File </mat-header-cell>
<mat-cell *matCellDef="let element" data-label="File"> {{element.fileName}} </mat-cell>
</ng-container>
在 ts:
改变
displayedColumns: string[] = ['File', 'Extension', 'Date', 'View', 'Delete'];
到
displayedColumns: string[] = ['fileName', 'Extension', 'Date', 'View', 'Delete'];
@user12129132 的答案将适用于正常排序(您需要更改数据源中的字段值以匹配列定义中的字段值,并在 matColumn 定义中使用相同的字段值)但是您的文件名中包含数字,它不会被排序 properly.For 我们可以使用 sortingDataAccessor 的目的。
在此处查看工作版本 - Stackblitz:https://stackblitz.com/edit/angular-g4svyj
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.sortingDataAccessor = (data, attribute) => {
let formattedData=data[attribute]
if(attribute=="fileName")
{
formattedData=parseInt(formattedData.match(/(\d)*$/)[0]);
}
return formattedData;
};
this.dataSource.paginator = this.paginator;
}
您需要将 MatSortModule
添加到 app.module.ts