Fixed Sorting and paging of Employees and Accounts
This commit is contained in:
@ -1,13 +1,12 @@
|
||||
import {DataSource} from '@angular/cdk/collections';
|
||||
import { DataSource } from '@angular/cdk/collections';
|
||||
import { MatPaginator } from '@angular/material/paginator';
|
||||
import { MatSort } from '@angular/material/sort';
|
||||
import {map, tap} from 'rxjs/operators';
|
||||
import {merge, Observable, of as observableOf} from 'rxjs';
|
||||
import {Employee} from '../employee';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
import { merge, Observable, of as observableOf } from 'rxjs';
|
||||
import { Employee } from '../employee';
|
||||
|
||||
|
||||
export class EmployeeListDataSource extends DataSource<Employee> {
|
||||
private dataObservable: Observable<Employee[]>;
|
||||
private filterValue: string;
|
||||
|
||||
constructor(private paginator: MatPaginator, private sort: MatSort, private filter: Observable<string>, public data: Employee[]) {
|
||||
@ -18,9 +17,8 @@ export class EmployeeListDataSource extends DataSource<Employee> {
|
||||
}
|
||||
|
||||
connect(): Observable<Employee[]> {
|
||||
this.dataObservable = observableOf(this.data);
|
||||
const dataMutations = [
|
||||
this.dataObservable,
|
||||
observableOf(this.data),
|
||||
this.filter,
|
||||
this.paginator.page,
|
||||
this.sort.sortChange
|
||||
@ -28,9 +26,13 @@ export class EmployeeListDataSource extends DataSource<Employee> {
|
||||
|
||||
return merge(...dataMutations).pipe(
|
||||
map((x: any) => {
|
||||
return this.getPagedData(this.getSortedData(this.getFilteredData([...this.data])));
|
||||
return this.getFilteredData([...this.data]);
|
||||
}),
|
||||
tap((x: Employee[]) => this.paginator.length = x.length)
|
||||
).pipe(
|
||||
map((x: any) => {
|
||||
return this.getPagedData(this.getSortedData(x));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -42,7 +44,7 @@ export class EmployeeListDataSource extends DataSource<Employee> {
|
||||
return filter.split(' ').reduce((p: Employee[], c: string) => {
|
||||
return p.filter(x => {
|
||||
const employeeString = (
|
||||
x.code + ' ' + x.name + ' ' + x.designation + ' ' + x.costCentre + (x.isActive ? ' active' : ' deactive')
|
||||
x.code + ' ' + x.name + ' ' + x.designation + ' ' + x.costCentre + (x.isActive ? ' active' : ' inactive')
|
||||
).toLowerCase();
|
||||
return employeeString.indexOf(c) !== -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user