5 sorting pagination angular-material2 angular
我正在尝试使用md-table实现排序,过滤和分页.这是我的代码:
connect(): Observable<Patient[]> {
const displayPatientDataChanges = [
this._patientDatabase.dataChange,
this._filterPatientChange,
this._paginator.page,
this._sort.mdSortChange,
];
return Observable.merge(...displayPatientDataChanges).map(() => {
const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
let displayData = this._patientDatabase.data.slice().filter((item: Patient) => {
let searchStr = (item.firstname + ' ' + item.lastname).toLowerCase();
return searchStr.indexOf(this.filter.toLowerCase()) != -1;
});
Run Code Online (Sandbox Code Playgroud)
我想返回这两个值,但它只返回过滤器和分页不起作用的排序函数.
return displayData.splice(startIndex, this._paginator.pageSize),this.getSortedData();
});
}
disconnect() { }
getSortedData(): Patient[] {
const data = this._patientDatabase.data.slice();
if (!this._sort.active || this._sort.direction == '') { return data; }
return data.sort((a, b) => {
let propertyA: number|string|Date = '';
let propertyB: number|string|Date = '';
switch (this._sort.active) {
case 'id': [propertyA, propertyB] = [a.id, b.id]; break;
case 'firstname': [propertyA, propertyB] = [a.firstname, b.firstname]; break;
case 'lastname': [propertyA, propertyB] = [a.lastname, b.lastname]; break;
case 'dateOfBirth': [propertyA, propertyB] = [a.dateOfBirth, b.dateOfBirth]; break;
case 'sex': [propertyA, propertyB]= [a.sex, b.sex]; break;
case 'dateAdded': [propertyA, propertyB] = [a.dateAdded, b.dateAdded]; break;
}
let valueA = isNaN(+propertyA) ? propertyA : +propertyA;
let valueB = isNaN(+propertyB) ? propertyB : +propertyB;
return (valueA < valueB ? -1 : 1) * (this._sort.direction == 'asc' ? 1 : 1);
});
}
Run Code Online (Sandbox Code Playgroud)
如何使排序,过滤和分页工作?
Lak*_*ton 18
编辑于01/29/2018:有关材料数据表的文章已经编写,可以在这里看到,它包括服务器分页,过滤和排序,可能比我下面的代码更新,因为很多人们正在访问这篇文章,我想有一个真正的需要,希望它会帮助你们所有人,玩得开心.
编辑:plunkr示例不再起作用,所以我在stackblitz上重新实现了最新的材料发布(截至此编辑,测试版12)注意:如果您使用的是较旧版本的材料,则需要将mat前缀更改为md
这个plunkr示例(不再起作用,请参阅上面的链接以获取工作版本)将向您显示包含您正在寻找的所有实现的表.
您可以看到它是如何通过依赖注入实现的:
const displayDataChanges = [
this._exampleDatabase.dataChange,
this._sort.mdSortChange,
this._filterChange,
this._paginator.page,
];
Run Code Online (Sandbox Code Playgroud)
该Observable.merge(...displayDataChanges).map过滤器中的数据,该sortData()方法排序,和分页程序产生的依赖于滤波的数据的长度的页数.