feat: add server-side pagination, sort and search to groups component (#507)

- Add server-side pagination, sort and search to groups component
- Refactor code to conform to current guidelines

See: #234
This commit is contained in:
punwai
2020-05-07 03:08:41 +05:30
committed by GitHub
parent 3136cd3b30
commit 4e95d57081
7 changed files with 281 additions and 123 deletions
+94 -25
View File
@@ -1,44 +1,113 @@
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
/** Angular Imports */
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { MatPaginator, MatSort, MatCheckbox } from '@angular/material';
import { FormControl } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
/** rxjs Imports */
import { merge } from 'rxjs';
import { tap, startWith, map, distinctUntilChanged, debounceTime} from 'rxjs/operators';
/** Custom Services */
import { GroupsService } from './groups.service';
import {DataSource} from '@angular/cdk/collections';
import { Observable } from 'rxjs';
/** Custom Data Source */
import { GroupsDataSource } from './groups.datasource';
/**
* Groups component.
*/
@Component({
selector: 'mifosx-app-groups',
templateUrl: './groups.component.html',
styleUrls: ['./groups.component.scss']
})
export class GroupsComponent implements OnInit {
export class GroupsComponent implements OnInit, AfterViewInit {
@ViewChild('showClosedGroups') showClosedGroups: MatCheckbox;
private ELEMENT_DATA: any = undefined;
displayedColumns = ['name', 'accountno', 'externalid', 'status', 'office'];
dataSource = new MatTableDataSource();
/** Name form control. */
name = new FormControl();
/** Columns to be displayed in groups table. */
displayedColumns = ['name', 'accountNo', 'externalId', 'status', 'officeName'];
/** Data source for groups table. */
dataSource: GroupsDataSource;
/** Groups filter. */
filterGroupsBy = [
{
type: 'name',
value: ''
}
];
/** Paginator for groups table. */
@ViewChild(MatPaginator) paginator: MatPaginator;
/** Sorter for groups table. */
@ViewChild(MatSort) sort: MatSort;
constructor(private groupService: GroupsService) { }
constructor(private groupsService: GroupsService) { }
ngOnInit() {
this.groupService.getGroups()
.subscribe(
(res => {
console.log(res);
// console.log(res.active);
res.active = !!res.active;
this.dataSource = new MatTableDataSource(res);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.getGroups();
}
/**
* Subscribes to all search filters:
* Name
* sort change and page change.
*/
ngAfterViewInit() {
this.name.valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged(),
tap((filterValue) => {
this.applyFilter(filterValue, 'name');
})
);
)
.subscribe();
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
tap(() => this.loadGroupsPage())
)
.subscribe();
}
changeShowClosedGroups() {
this.loadGroupsPage();
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim(); // Remove whitespace
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
this.dataSource.filter = filterValue;
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
/**
* Loads a page of groups.
*/
loadGroupsPage() {
if (!this.sort.direction) {
delete this.sort.active;
}
this.dataSource.getGroups(this.filterGroupsBy, this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize, !this.showClosedGroups.checked);
}
/**
* Filters data in groups table based on passed value and poperty.
* @param {string} filterValue Value to filter data.
* @param {string} property Property to filter data by.
*/
applyFilter(filterValue: string, property: string) {
this.paginator.pageIndex = 0;
const findIndex = this.filterGroupsBy.findIndex(filter => filter.type === property);
this.filterGroupsBy[findIndex].value = filterValue;
this.loadGroupsPage();
}
/**
* Initializes the data source for groups table and loads the first page.
*/
getGroups() {
this.dataSource = new GroupsDataSource(this.groupsService);
this.dataSource.getGroups(this.filterGroupsBy, this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize);
}
}