import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'; import { MatPaginator, MatSort, MatTableDataSource, MatCheckbox } from '@angular/material'; import { ClientsDataSource } from './clients.datasource'; /** rxjs Imports */ import { merge } from 'rxjs'; import { tap } from 'rxjs/operators'; /** Custom Services */ import { ClientsService } from './clients.service'; @Component({ selector: 'mifosx-clients', templateUrl: './clients.component.html', styleUrls: ['./clients.component.scss'], }) export class ClientsComponent implements OnInit, AfterViewInit { @ViewChild('showClosedAccounts') showClosedAccounts: MatCheckbox; displayedColumns = ['name', 'clientno', 'externalid', 'status', 'mobileNo', 'gender', 'office', 'staff']; dataSource: ClientsDataSource; @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; constructor(private clientsService: ClientsService) { } ngOnInit() { this.getClients(); } ngAfterViewInit() { this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0); merge(this.sort.sortChange, this.paginator.page, this.showClosedAccounts.change) .pipe( tap(() => this.loadClientsPage()) ) .subscribe(); } /** * Loads a page of journal entries. */ loadClientsPage() { if (!this.sort.direction) { delete this.sort.active; } this.dataSource.getClients(this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize, !this.showClosedAccounts.checked); } /** * Initializes the data source for clients table and loads the first page. */ getClients() { this.dataSource = new ClientsDataSource(this.clientsService); this.dataSource.getClients(this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize, !this.showClosedAccounts.checked); } }