如何在角度为7的情况下使用DataTable

时间:2019-08-08 14:11:57

标签: angular angular-material

我正在使用DataTable设置员工表,虽然出现了DataTable,但是分页,筛选和排序不起作用,控制台中没有出现错误。

Ps :我正在使用角度7

感谢帮助

Employees-List.ts:

import { Component, OnInit , Inject , ViewChild} from '@angular/core';
import { RestApiService } from "../shared/rest-api.service";
import { Router , ActivatedRoute} from '@angular/router';
import { Employee } from '../shared/employee';
import { MatTableDataSource, MatSort, MatPaginator } from 
'@angular/material';  
import { DatatableComponent } from '@swimlane/ngx-datatable';
@Component({
selector: 'app-employees-list',
templateUrl: './employees-list.component.html',
styleUrls: ['./employees-list.component.css']
})
export class EmployeesListComponent implements OnInit {


dataSource : any =[];

displayedColumns: string[] = ['id', 'name', 'email', 'phone'];  
@ViewChild(DatatableComponent , {static : true}) table: 
DatatableComponent;
@ViewChild(MatPaginator , { static: true }) paginator: MatPaginator;  
@ViewChild(MatSort , { static: true }) sort: MatSort;  
Employee: any = [];
actRoute: ActivatedRoute;
items : any = [];

constructor(
public restApi: RestApiService,
public router: Router
) {
this.restApi.getEmployees().subscribe(data1 =>{  
  this.dataSource= data1;

});
 }

ngOnInit() {
this.loadEmployees();
this.dataSource.paginator = this.paginator;  
this.dataSource.sort = this.sort;  
}

applyFilter(filterValue: string) {  
this.dataSource.filter = filterValue.trim().toLowerCase();  

if (this.dataSource.paginator) {  
  this.dataSource.paginator.firstPage();  
}  
}  

// Get employees list
loadEmployees() {
return this.restApi.getEmployees().subscribe(datat => {
  this.Employee = datat;
  this.items=datat;
})
}

// Delete employee
deleteEmployee(id) {
if (window.confirm('Are you sure, you want to delete?')){
  this.restApi.deleteEmployee(id).subscribe(datax => {
    this.loadEmployees()
  })
}
}  

}

Employees-List.html:

<h2>Employees List</h2>  
<mat-form-field>  
<input matInput (keyup)="applyFilter($event.target.value)" 
placeholder="Filter">  
</mat-form-field>  

<div class="mat-elevation-z8">  
<table mat-table [dataSource]="dataSource" matSort>  

<ng-container matColumnDef="id">  
  <th mat-header-cell *matHeaderCellDef mat-sort-header> employee id</th>  
  <td mat-cell *matCellDef="let employee"> {{employee.id}} </td>  
</ng-container>  

<ng-container matColumnDef="name">  
  <th mat-header-cell *matHeaderCellDef mat-sort-header>  Employee Name 
</th>  
  <td mat-cell *matCellDef="let employee"> {{employee.name}} </td>  
</ng-container>  

    <ng-container matColumnDef="email">  
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Email </th>  
      <td mat-cell *matCellDef="let employee"> {{employee.email}} </td>  
    </ng-container>  

<ng-container matColumnDef="phone">  
  <th mat-header-cell *matHeaderCellDef mat-sort-header> phone </th>  
  <td mat-cell *matCellDef="let employee"> {{employee.phone}} </td>  
</ng-container>  


<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>  
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>  
</table>  

<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>  
</div>

this is what I found when I run it ! the pagination , sorting and filtering does'nt work

1 个答案:

答案 0 :(得分:0)

在Employees-List.ts文件中,您必须在初始化数据之类后添加分页和排序

this.restApi.getEmployees().subscribe(data1 =>{  
  this.dataSource= new MatTableDataSource(data1);
  this.dataSource.paginator = this.paginator;  
  this.dataSource.sort = this.sort;  
});

此外,您还必须根据席子表将this.dataSource= data1更改为this.dataSource= new MatTableDataSource(data1)