feat: add server-side pagination sort and search for centers component (#506)
- Add server-side pagination, sort and search for centers component - Refactor code to meet current guidelines See: #234
This commit is contained in:
@@ -1,45 +1,132 @@
|
||||
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
|
||||
import {MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
|
||||
import { CentersService } from 'app/centers/centers.service';
|
||||
import {DataSource} from '@angular/cdk/collections';
|
||||
import { Observable } from 'rxjs';
|
||||
/** 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 { CentersService } from './centers.service';
|
||||
|
||||
/** Custom Data Source */
|
||||
import { CentersDataSource } from './centers.datasource';
|
||||
|
||||
/**
|
||||
* Centers component.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'mifosx-app-centers',
|
||||
templateUrl: './centers.component.html',
|
||||
styleUrls: ['./centers.component.scss'],
|
||||
|
||||
})
|
||||
export class CentersComponent implements OnInit {
|
||||
private ELEMENT_DATA: any = undefined;
|
||||
displayedColumns = ['name', 'accountno', 'externalid', 'status', 'office'];
|
||||
dataSource = new MatTableDataSource();
|
||||
export class CentersComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('showClosedCenters') showClosedCenters: MatCheckbox;
|
||||
|
||||
/** Name form control. */
|
||||
name = new FormControl();
|
||||
/** ExternalId form control. */
|
||||
externalId = new FormControl();
|
||||
/** Columns to be displayed in centers table. */
|
||||
displayedColumns = ['name', 'accountNo', 'externalId', 'status', 'officeName'];
|
||||
/** Data source for centers table. */
|
||||
dataSource: CentersDataSource;
|
||||
/** Centers filter. */
|
||||
filterCentersBy = [
|
||||
{
|
||||
type: 'name',
|
||||
value: ''
|
||||
},
|
||||
{
|
||||
type: 'externalId',
|
||||
value: ''
|
||||
}
|
||||
];
|
||||
|
||||
/** Paginator for centers table. */
|
||||
@ViewChild(MatPaginator) paginator: MatPaginator;
|
||||
/** Sorter for centers table. */
|
||||
@ViewChild(MatSort) sort: MatSort;
|
||||
constructor(private centerService: CentersService) { }
|
||||
|
||||
constructor(private centersService: CentersService) { }
|
||||
|
||||
ngOnInit() {
|
||||
this.centerService.getCenters()
|
||||
.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.getCenters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to all search filters:
|
||||
* Name, ExternalId
|
||||
* sort change and page change.
|
||||
*/
|
||||
ngAfterViewInit() {
|
||||
|
||||
this.name.valueChanges
|
||||
.pipe(
|
||||
debounceTime(500),
|
||||
distinctUntilChanged(),
|
||||
tap((filterValue) => {
|
||||
this.applyFilter(filterValue, 'name');
|
||||
})
|
||||
);
|
||||
)
|
||||
.subscribe();
|
||||
|
||||
this.externalId.valueChanges
|
||||
.pipe(
|
||||
debounceTime(500),
|
||||
distinctUntilChanged(),
|
||||
tap((filterValue) => {
|
||||
this.applyFilter(filterValue, 'externalId');
|
||||
})
|
||||
)
|
||||
.subscribe();
|
||||
|
||||
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
|
||||
|
||||
merge(this.sort.sortChange, this.paginator.page)
|
||||
.pipe(
|
||||
tap(() => this.loadCentersPage())
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads page on changing show closed centers checkbox
|
||||
*/
|
||||
changeShowClosedCenters() {
|
||||
this.loadCentersPage();
|
||||
}
|
||||
|
||||
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 centers.
|
||||
*/
|
||||
loadCentersPage() {
|
||||
if (!this.sort.direction) {
|
||||
delete this.sort.active;
|
||||
}
|
||||
this.dataSource.getCenters(this.filterCentersBy, this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize, !this.showClosedCenters.checked);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters data in centers 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.filterCentersBy.findIndex(filter => filter.type === property);
|
||||
this.filterCentersBy[findIndex].value = filterValue;
|
||||
this.loadCentersPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the data source for centers table and loads the first page.
|
||||
*/
|
||||
getCenters() {
|
||||
this.dataSource = new CentersDataSource(this.centersService);
|
||||
this.dataSource.getCenters(this.filterCentersBy, this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user