Fixed Sorting and paging of Employees and Accounts

This commit is contained in:
Amritanshu Agrawal 2020-05-30 16:18:08 +05:30
parent d5bc818632
commit dd799a8e2f
2 changed files with 31 additions and 18 deletions
overlord/src/app
account/account-list
employee/employee-list

@ -1,13 +1,13 @@
import {DataSource} from '@angular/cdk/collections'; import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator'; import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort'; import { MatSort } from '@angular/material/sort';
import {map, tap} from 'rxjs/operators'; import { map, tap } from 'rxjs/operators';
import {merge, Observable, of as observableOf} from 'rxjs'; import { merge, Observable, of as observableOf } from 'rxjs';
import {Account} from '../../core/account'; import { Account } from '../../core/account';
import {Employee} from "../../employee/employee";
export class AccountListDataSource extends DataSource<Account> { export class AccountListDataSource extends DataSource<Account> {
private dataObservable: Observable<Account[]>;
private filterValue: string; private filterValue: string;
constructor(private paginator: MatPaginator, private sort: MatSort, private filter: Observable<string>, public data: Account[]) { constructor(private paginator: MatPaginator, private sort: MatSort, private filter: Observable<string>, public data: Account[]) {
@ -18,9 +18,8 @@ export class AccountListDataSource extends DataSource<Account> {
} }
connect(): Observable<Account[]> { connect(): Observable<Account[]> {
this.dataObservable = observableOf(this.data);
const dataMutations = [ const dataMutations = [
this.dataObservable, observableOf(this.data),
this.filter, this.filter,
this.paginator.page, this.paginator.page,
this.sort.sortChange this.sort.sortChange
@ -28,9 +27,13 @@ export class AccountListDataSource extends DataSource<Account> {
return merge(...dataMutations).pipe( return merge(...dataMutations).pipe(
map((x: any) => { map((x: any) => {
return this.getPagedData(this.getSortedData(this.getFilteredData([...this.data]))); return this.getFilteredData([...this.data]);
}), }),
tap((x: Account[]) => this.paginator.length = x.length) tap((x: Account[]) => this.paginator.length = x.length)
).pipe(
map((x: any) => {
return this.getPagedData(this.getSortedData(x));
})
); );
} }
@ -42,7 +45,7 @@ export class AccountListDataSource extends DataSource<Account> {
return filter.split(' ').reduce((p: Account[], c: string) => { return filter.split(' ').reduce((p: Account[], c: string) => {
return p.filter(x => { return p.filter(x => {
const accountString = ( const accountString = (
x.name + ' ' + x.type + ' ' + x.costCentre + (x.isActive ? ' active' : ' deactive') x.name + ' ' + x.type + ' ' + x.costCentre + (x.isActive ? ' active' : ' inactive')
).toLowerCase(); ).toLowerCase();
return accountString.indexOf(c) !== -1; return accountString.indexOf(c) !== -1;
} }
@ -65,6 +68,14 @@ export class AccountListDataSource extends DataSource<Account> {
switch (this.sort.active) { switch (this.sort.active) {
case 'name': case 'name':
return compare(a.name, b.name, isAsc); return compare(a.name, b.name, isAsc);
case 'type':
return compare(a.type, b.type, isAsc);
case 'isActive':
return compare(a.isActive, b.isActive, isAsc);
case 'isReconcilable':
return compare(a.isReconcilable, b.isReconcilable, isAsc);
case 'costCentre':
return compare(a.costCentre, b.costCentre, isAsc);
case 'id': case 'id':
return compare(+a.id, +b.id, isAsc); return compare(+a.id, +b.id, isAsc);
default: default:

@ -1,13 +1,12 @@
import {DataSource} from '@angular/cdk/collections'; import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator'; import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort'; import { MatSort } from '@angular/material/sort';
import {map, tap} from 'rxjs/operators'; import { map, tap } from 'rxjs/operators';
import {merge, Observable, of as observableOf} from 'rxjs'; import { merge, Observable, of as observableOf } from 'rxjs';
import {Employee} from '../employee'; import { Employee } from '../employee';
export class EmployeeListDataSource extends DataSource<Employee> { export class EmployeeListDataSource extends DataSource<Employee> {
private dataObservable: Observable<Employee[]>;
private filterValue: string; private filterValue: string;
constructor(private paginator: MatPaginator, private sort: MatSort, private filter: Observable<string>, public data: Employee[]) { 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[]> { connect(): Observable<Employee[]> {
this.dataObservable = observableOf(this.data);
const dataMutations = [ const dataMutations = [
this.dataObservable, observableOf(this.data),
this.filter, this.filter,
this.paginator.page, this.paginator.page,
this.sort.sortChange this.sort.sortChange
@ -28,9 +26,13 @@ export class EmployeeListDataSource extends DataSource<Employee> {
return merge(...dataMutations).pipe( return merge(...dataMutations).pipe(
map((x: any) => { 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) 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 filter.split(' ').reduce((p: Employee[], c: string) => {
return p.filter(x => { return p.filter(x => {
const employeeString = ( 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(); ).toLowerCase();
return employeeString.indexOf(c) !== -1; return employeeString.indexOf(c) !== -1;
} }