I'm trying to implement sort, filter and pagination using md-table. here is my code:
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;
});
I want to return both of these values but it only returns the sort function the filter and the pagination don't work.
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);
});
}
How can I make the sort, filter and pagination to work?