英文:
sorting based on column click in angular material with hidden headers
问题
I am using angular material version 15, here i need to sort the table but without headers, so based on click of column, sorting must take place.
HTML:
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element" (click)="sortData('name')"> {{element.name}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Ts:
sortData(event) {
let booleanValue: boolean = false;
let boolean: any;
if (boolean == true) {
this.dataSource.sort((a, b) => a[event] < b[event] ? 1 : a[event] > b[event] ? -1 : 0);
booleanValue = !booleanValue;
boolean = booleanValue;
} else {
this.dataSource.sort((a, b) => a[event] > b[event] ? 1 : a[event] < b[event] ? -1 : 0);
booleanValue = !booleanValue;
boolean = booleanValue;
}
}
英文:
I am using angular material version 15, here i need to sort the table but without headers, so based on click of column, sorting must take place.
HTML:
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element" (click)="sortData('name')"> {{element.name}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Ts:
sortData(event) {
let booleanValue:boolean = false;
let boolean:any;
if (boolean == true){
this.dataSource.sort((a, b) => a[event] < b[event] ? 1 : a[event] > b[event] ? -1 : 0)
booleanValue = !booleanValue
boolean = booleanValue
}
else{
this.dataSource.sort((a, b) => a[event] > b[event] ? 1 : a[event] < b[event] ? -1 : 0)
booleanValue = !booleanValue
boolean = booleanValue
}
}
答案1
得分: 1
可以使用一个对象来进行排序,就像Material Angular中的使用方式一样:
sort={active: '', direction: 'asc'}
然后,您的sortData
函数会使用这个对象。函数会变成这样:
sortData(event: string) {
this.sort.direction = this.sort.active == event ? this.sort.direction == 'asc' ? 'desc' : 'asc' : 'asc';
this.sort.active = event;
const factor = this.sort.direction == 'asc' ? 1 : -1;
this.dataSource.sort((a: any, b: any) => a[event] < b[event] ? factor :
a[event] > b[event] ? -factor : 0);
this.table.renderRows();
}
请注意,需要使用renderRows()
方法来刷新表格。要获取表格,可以使用ViewChild
:
@ViewChild(MatTable) table: MatTable<any>;
英文:
You can use an object sort like material angular use
sort={active:'',direction:'asc'}
Then your function sortData
use this object. Teh function will become like
sortData(event:string) {
this.sort.direction=this.sort.active==event?this.sort.direction=='asc'?'desc':
'asc':'asc'
this.sort.active=event
const factor=this.sort.direction=='asc'?1:-1;
this.dataSource.sort((a:any, b:any) => a[event] < b[event] ? factor :
a[event] > b[event] ? -factor : 0)
this.table.renderRows()
}
See that it's necesary use renderRows()
. To get the table use ViewChild
@ViewChild(MatTable) table: MatTable<any>;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论