Angular 6 - 对所有 table 列进行排序仅适用于两列而不是所有列
Angular 6 - Sorting all table columns works on only two columns instead of all
我正在尝试用 angular material 的 MatSort
对 MatTable
进行排序,但问题是我只能对 MatTable
的两列进行排序,但我想要的是是对我所有的列进行排序,我不明白为什么它不起作用...我可以单击箭头,就像我想对 table 的数据进行排序一样,但是除了列 [=17] 之外没有任何反应=] 和 occupation
排序很好。
这是我的代码(我正在将 MatSortModule
导入我的 app.module.ts
):
csr.component.ts
import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {PosteCSRPoste, PosteCSRService} from '../../api';
import {MatSort, MatTableDataSource} from '@angular/material';
@Component({
selector: 'app-csr',
templateUrl: './csr.component.html',
styleUrls: [
'./csr.component.css',
'../app.component.css'
]
})
export class CsrComponent implements OnInit, AfterViewInit {
displayedColumns = [
'disciplineCsd',
'disciplineCsr',
'multiple',
'communeIdentique',
'etablissementCsd',
'etablissementCsr',
'occupation'
];
postes = new MatTableDataSource<PosteCSRPoste>();
@ViewChild(MatSort) sort: MatSort;
constructor(private posteCSRService: PosteCSRService) {
}
ngOnInit(): void {
this.getPostesCSR();
}
ngAfterViewInit(): void {
this.postes.sort = this.sort;
}
getPostesCSR(): void {
this.posteCSRService.getPosteCSRCollection().subscribe(data => {
this.postes.data = data['hydra:member'];
});
}
}
csr.component.html
<table mat-table [dataSource]="postes" matSort class="table table-hover table-bordered mat-elevation-z8">
<ng-container matColumnDef="disciplineCsd">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Discipline CSD</th>
<td mat-cell *matCellDef="let poste">{{ poste.disciplineCsd.nom | capitalize }}</td>
</ng-container>
<ng-container matColumnDef="disciplineCsr">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Discipline CSR</th>
<td mat-cell *matCellDef="let poste">{{ poste.disciplineCsr.nom | capitalize }}</td>
</ng-container>
<ng-container matColumnDef="multiple">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Multiple</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.multiple; else nonMultiple">
Multiple
</ng-container>
<ng-template #nonMultiple>
Non multiple
</ng-template>
</td>
</ng-container>
<ng-container matColumnDef="communeIdentique">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Même commune</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.communesIdentiques; else communesDifferentes">
Mêmes communes
</ng-container>
<ng-template #communesDifferentes>
Communes différentes
</ng-template>
</td>
</ng-container>
<ng-container matColumnDef="etablissementCsd">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Établissement CSD</th>
<td mat-cell *matCellDef="let poste">{{ poste.etablissementCsd.nom }}</td>
</ng-container>
<ng-container matColumnDef="etablissementCsr">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Établissement CSR</th>
<td mat-cell *matCellDef="let poste">{{ poste.etablissementCsr.nom }}</td>
</ng-container>
<ng-container matColumnDef="occupation">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Occupation</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.occupation; else vacant">
Occupé
</ng-container>
<ng-template #vacant>
Vacant
</ng-template>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
请告诉我哪里做错了。
这个解决了我的问题。
matColumnDef
属性 必须与您的模型同名
我正在尝试用 angular material 的 MatSort
对 MatTable
进行排序,但问题是我只能对 MatTable
的两列进行排序,但我想要的是是对我所有的列进行排序,我不明白为什么它不起作用...我可以单击箭头,就像我想对 table 的数据进行排序一样,但是除了列 [=17] 之外没有任何反应=] 和 occupation
排序很好。
这是我的代码(我正在将 MatSortModule
导入我的 app.module.ts
):
csr.component.ts
import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {PosteCSRPoste, PosteCSRService} from '../../api';
import {MatSort, MatTableDataSource} from '@angular/material';
@Component({
selector: 'app-csr',
templateUrl: './csr.component.html',
styleUrls: [
'./csr.component.css',
'../app.component.css'
]
})
export class CsrComponent implements OnInit, AfterViewInit {
displayedColumns = [
'disciplineCsd',
'disciplineCsr',
'multiple',
'communeIdentique',
'etablissementCsd',
'etablissementCsr',
'occupation'
];
postes = new MatTableDataSource<PosteCSRPoste>();
@ViewChild(MatSort) sort: MatSort;
constructor(private posteCSRService: PosteCSRService) {
}
ngOnInit(): void {
this.getPostesCSR();
}
ngAfterViewInit(): void {
this.postes.sort = this.sort;
}
getPostesCSR(): void {
this.posteCSRService.getPosteCSRCollection().subscribe(data => {
this.postes.data = data['hydra:member'];
});
}
}
csr.component.html
<table mat-table [dataSource]="postes" matSort class="table table-hover table-bordered mat-elevation-z8">
<ng-container matColumnDef="disciplineCsd">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Discipline CSD</th>
<td mat-cell *matCellDef="let poste">{{ poste.disciplineCsd.nom | capitalize }}</td>
</ng-container>
<ng-container matColumnDef="disciplineCsr">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Discipline CSR</th>
<td mat-cell *matCellDef="let poste">{{ poste.disciplineCsr.nom | capitalize }}</td>
</ng-container>
<ng-container matColumnDef="multiple">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Multiple</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.multiple; else nonMultiple">
Multiple
</ng-container>
<ng-template #nonMultiple>
Non multiple
</ng-template>
</td>
</ng-container>
<ng-container matColumnDef="communeIdentique">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Même commune</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.communesIdentiques; else communesDifferentes">
Mêmes communes
</ng-container>
<ng-template #communesDifferentes>
Communes différentes
</ng-template>
</td>
</ng-container>
<ng-container matColumnDef="etablissementCsd">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Établissement CSD</th>
<td mat-cell *matCellDef="let poste">{{ poste.etablissementCsd.nom }}</td>
</ng-container>
<ng-container matColumnDef="etablissementCsr">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Établissement CSR</th>
<td mat-cell *matCellDef="let poste">{{ poste.etablissementCsr.nom }}</td>
</ng-container>
<ng-container matColumnDef="occupation">
<th mat-header-cell *matHeaderCellDef mat-sort-header scope="col">Occupation</th>
<td mat-cell *matCellDef="let poste">
<ng-container *ngIf="poste.occupation; else vacant">
Occupé
</ng-container>
<ng-template #vacant>
Vacant
</ng-template>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
请告诉我哪里做错了。
这个
matColumnDef
属性 必须与您的模型同名