我已经在mat-table中实现了角度8的排序。我已经参考了https://material.angular.io/components/sort/overview上的指南,但是排序仅适用于“ id”,“ email”,“ contact details”等几列”,“国家”和“部门”。 下面是代码:
Home.component.html:
<div class="empdata">
<h3>
Employee List:
</h3>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- ID column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Id </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<!-- First Name Column -->
<ng-container matColumnDef="fname">
<th mat-header-cell *matHeaderCellDef mat-sort-header> First Name </th>
<td mat-cell *matCellDef="let element"> {{element.firstName}} </td>
</ng-container>
<!-- Last Name Column -->
<ng-container matColumnDef="lname">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Last Name </th>
<td mat-cell *matCellDef="let element"> {{element.lastName}} </td>
</ng-container>
<!-- Email Column -->
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Email </th>
<td mat-cell *matCellDef="let element"> {{element.email}} </td>
</ng-container>
<!-- Phone Column -->
<ng-container matColumnDef="phone">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Contact Details </th>
<td mat-cell *matCellDef="let element"> {{element.phone}} </td>
</ng-container>
<!--Country Column -->
<ng-container matColumnDef="country">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Country </th>
<td mat-cell *matCellDef="let element"> {{element.country}} </td>
</ng-container>
<!-- department Column -->
<ng-container matColumnDef="department">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Department </th>
<td mat-cell *matCellDef="let element"> {{element.deptName}} </td>
</ng-container>
<!-- Actions Column -->
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef> Actions </th>
<td mat-cell *matCellDef="let element">
<button color="accent" (click)="update(element.id)" mat-raised-button>Update</button>
<button color="accent" (click)="delete(element.id)" mat-raised-button>Delete</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
2。 Home.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { EmployeeService } from '../employee.service';
import { Router } from '@angular/router';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { IEmployee } from '../employee';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
//public employees = [];
public dataSource; //= new MatTableDataSource<IEmployee>();;
@ViewChild(MatSort, {static: true}) sort: MatSort;
displayedColumns: string[] = ['id', 'fname', 'lname', 'email', 'phone', 'country','department', 'actions'];
constructor(private _empService: EmployeeService, private router: Router) { }
ngOnInit() {
this._empService.getEmployees().subscribe(
data => {
this.dataSource = new MatTableDataSource();
this.dataSource.data = data;
this.dataSource.sort = this.sort;
}
);
}
update(id){
this.dataSource.data.forEach((emp) => {
if( emp.id == id){
this._empService.updateEmp = emp;
// console.log("in if condition..")
this.router.navigate(['register']);
}
});
}
delete(id){
this._empService.deleteEmployee(id).subscribe(
data => {
console.log("Delete request is successful");
this._empService.status = "deleted";
this.router.navigate(['update-success']);
}
)
}
}
有人可以告诉我我在做什么错吗?
答案 0 :(得分:0)
请看看这个:
默认情况下,MatTableDataSource的排序假设是已排序列的名称与该列显示的数据属性名称匹配。
(写在文档中)
这意味着在您的代码中...
<ng-container matColumnDef="fname">
此fname应该更改为firstName以满足上述要求。与姓氏相同。