angular - MatTableDatasource 的自定义排序数据源:首先选中复选框
问题描述
我需要对 a 进行排序MatTableDataSource
,以便首先显示选中复选框的行,然后显示未选中的行。
我看到排序datasource
取决于MatColumnDef
标题名称(id)和 asc|desc(开始),但这对我没有帮助。我试图排序,dataSource.data
因为我有isSelected
.
这是我尝试过的,但不起作用
HTML
<button (click)="sortDataSource('createdDate', 'asc')">
<span>isSelected</span><mat-icon>arrow_upward</mat-icon>
</button>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null" [checked]="selection.hasValue() && isAllSelected()" [indeterminate]="selection.hasValue() && !isAllSelected()" [aria-label]="checkboxLabel()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? singleToggle(row) : null" [checked]="selection.isSelected(row)" [aria-label]="checkboxLabel(row)">
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="privId">
...
打字稿
@ViewChild(MatSort, { static: false }) matSort: MatSort;
sortDataSource(id: string, start: 'asc' | 'desc') {
const matSort = this.dataSource.sort;
this.dataSource.data.sort((a: any, b: any) => {
if (a.isSelected && !b.isSelected) {
return -1;
} else if (!a.isSelected && b.isSelected) {
return 1;
} else {
return 0;
}
});
this.dataSource.sort = this.matSort;
}
query(q?) {
this.privs = [];
if (q) {
this.query = q;
}
this.neo4jService.httpNeo4jRun(this.query, null)
.subscribe((r) => {
// console.log(r); // this.checkBox);
JSON.parse(JSON.stringify(r.records)).forEach(node => {
this.privs.push(node._fields[0].properties)
});
this.dataSource = new MatTableDataSource(this.privs);
if (this.selectedPriv && this.selectedPriv.length > 0) {
this.selection = new SelectionModel < Priv > (true, this.privs.filter(p => this.selectedPriv.find(e => e.privCode == p.privCode) ? true : false));
}
this.dataSource.sort = this.sort;
// this.sortDataSource('privName', 'asc');
this.dataSource.paginator = this.paginator;
});
}
目前我手动触发sortDataSource
但理想情况下我想在服务器的所有记录都被推送到集合中之后在查询方法中执行它。
谢谢你的帮助
解决方案
推荐阅读
- azure - 当 Kusto 中的数据存在差距时,仅使用连续数据集和截断数据
- spring-boot - Spring Cloud Gateway - 找不到名称为 [Filter_Name] 的 GatewayFilterFactory
- r - 用于计算字符数 Spark R 的语法
- mongodb - 如何根据数组是否包含pymongo中的特定元素来更新所有文档?
- python - For循环在Python中迭代一个没有顺序的列表
- arrays - 将一个命令的结果传递给另一个命令
- mysql - 在插入之前创建 mysql 触发器(语法错误)
- angular - 如何在 Angular 9 中导入 automapper-ts
- wpf - 如何使用 tooltip 样式中的 depenceny 属性来设置文本?
- bash - 使用通用密码自动化 ssh-add 私钥