Skip to content

Commit

Permalink
feat: add custom data source
Browse files Browse the repository at this point in the history
Add custom data source to implement server side pagination, sorting and
filtering.
  • Loading branch information
abhaychawla committed Jul 26, 2018
1 parent 1a164c1 commit bbbe772
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 45 deletions.
20 changes: 18 additions & 2 deletions src/app/accounting/accounting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,24 @@ export class AccountingService {
return this.http.get(`/journalentries`, { params: httpParams });
}

getJournalEntries() {
return this.http.get('/journalentries');
// filterBy: officeId, glAccountId, manualEntriesOnly, fromDate, toDate, transactionId
getJournalEntries(filterBy: any, orderBy: any, sortBy: any, offset: number, limit: number) {
console.log('Order By:', orderBy);
console.log('Sort By:', sortBy);
console.log('Filter By:', filterBy);
let httpParams = new HttpParams()
.set('offset', offset.toString())
.set('limit', limit.toString())
.set('sortBy', sortBy)
.set('orderBy', orderBy);
filterBy.forEach(function (filter: any) {
if(filter.value) {
httpParams = httpParams.set(filter.type, filter.value);
}
});
return this.http.get('/journalentries', {
params: httpParams
});
}

revertTransaction(transactionId: string, comments: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CollectionViewer, DataSource } from "@angular/cdk/collections";

import { Observable, BehaviorSubject } from "rxjs";

import { AccountingService } from "../accounting.service";

export class JournalEntriesDataSource implements DataSource<any> {

private journalEntriesSubject = new BehaviorSubject<any[]>([]);
private recordsSubject = new BehaviorSubject<number>(0);

public records$ = this.recordsSubject.asObservable();

constructor(private accountingService: AccountingService) { }

loadJournalEntries(filterBy: any, orderBy = '', sortBy = '', offset = 0, limit = 10) {
this.journalEntriesSubject.next([]);
orderBy = (orderBy === 'debit' || orderBy === 'credit') ? 'amount' : orderBy;
this.accountingService.getJournalEntries(filterBy, orderBy, sortBy, offset, limit)
.subscribe((journalEntries: any) => {
console.log(journalEntries);
this.recordsSubject.next(journalEntries.totalFilteredRecords);
this.journalEntriesSubject.next(journalEntries.pageItems);
});
}

connect(collectionViewer: CollectionViewer): Observable<any[]> {
return this.journalEntriesSubject.asObservable();
}

disconnect(collectionViewer: CollectionViewer): void {
this.journalEntriesSubject.complete();
this.recordsSubject.complete();
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<div class="container" fxLayout="row wrap" fxLayoutGap="2%">
<mat-form-field fxFlex="31%">
<mat-select placeholder="Office" [formControl]="officeName" (selectionChange)="applyFilter($event.value, 'officeName')">
<mat-option *ngFor="let office of officeData" [value]="office.name">
<mat-select placeholder="Office" [formControl]="officeName" (selectionChange)="applyFilter($event.value, 'officeId')">
<mat-option *ngFor="let office of officeData" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="31%">
<mat-select placeholder="Affected GL Entry" [formControl]="glAccountCode" (selectionChange)="applyFilter($event.value, 'glAccountCode')">
<mat-option *ngFor="let affectedGLEntryDebit of glAccountData" [value]="affectedGLEntryDebit.glCode">
{{ affectedGLEntryDebit.name + ' (' + affectedGLEntryDebit.glCode + ')' }}
<mat-select placeholder="Affected GL Entry" [formControl]="glAccountCode" (selectionChange)="applyFilter($event.value, 'glAccountId')">
<mat-option *ngFor="let affectedGLEntry of glAccountData" [value]="affectedGLEntry.id">
{{ affectedGLEntry.name + ' (' + affectedGLEntry.glCode + ')' }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="31%">
<mat-select placeholder="Filter" [formControl]="filter" (selectionChange)="applyFilter($event.value, 'filter')">
<mat-select placeholder="Filter" [formControl]="filter" (selectionChange)="applyFilter($event.value, 'manualEntriesOnly')">
<mat-option *ngFor="let filter of filterData" [value]="filter.value">
{{ filter.option }}
</mat-option>
Expand All @@ -33,13 +33,6 @@
<mat-form-field fxFlex="31%">
<input matInput placeholder="Transaction ID" [formControl]="transactionId" (keyup)="applyFilter($event.target.value, 'transactionId')">
</mat-form-field>
<!-- <mat-form-field fxFlex="33%">
<mat-select placeholder="Transaction ID" [formControl]="transactionId">
<mat-option *ngFor="let transaction of dataSource?.data" [value]="transaction.transactionId">
{{ transaction.transactionId }}
</mat-option>
</mat-select>
</mat-form-field> -->
</div>

<div class="mat-elevation-z8 container">
Expand Down Expand Up @@ -67,12 +60,12 @@
</ng-container>

<ng-container matColumnDef="glAccountType">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Type </th>
<th mat-header-cell *matHeaderCellDef> Type </th>
<td mat-cell *matCellDef="let transaction"> {{ transaction.glAccountType.value }} </td>
</ng-container>

<ng-container matColumnDef="createdByUserName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Created by </th>
<th mat-header-cell *matHeaderCellDef> Created by </th>
<td mat-cell *matCellDef="let transaction"> {{ transaction.createdByUserName }} </td>
</ng-container>

Expand Down Expand Up @@ -101,6 +94,6 @@

</table>

<mat-paginator [pageSizeOptions]="[10, 25, 50, 100]" showFirstLastButtons></mat-paginator>
<mat-paginator [length]="dataSource?.records$ | async" [pageSize]="10" [pageSizeOptions]="[10, 25, 50, 100]" showFirstLastButtons></mat-paginator>

</div>
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { MatPaginator, MatSort } from '@angular/material';
import { FormControl } from '@angular/forms';
import { Router } from '@angular/router';

import { merge } from 'rxjs';
import { tap} from 'rxjs/operators';

import { AccountingService } from '../accounting.service';
import { JournalEntriesDataSource } from './journal-entry.datasource';

@Component({
selector: 'mifosx-search-journal-entry',
Expand All @@ -12,13 +16,14 @@ import { AccountingService } from '../accounting.service';
})
export class SearchJournalEntryComponent implements OnInit {

// TODO: Update when date and language are set up throughout the application
minDate = new Date(2000, 0, 1);
maxDate = new Date();
officeName = new FormControl();
glAccountCode = new FormControl();
transactionDateFrom = new FormControl(new Date(2000, 0, 1));
transactionDateTo = new FormControl(new Date());
transactionId = new FormControl();
minDate = new Date(2000, 0, 1);
maxDate = new Date();
filter = new FormControl('');
displayedColumns: string[] = ['id', 'officeName', 'transactionId', 'transactionDate', 'glAccountType', 'createdByUserName', 'glAccountCode', 'glAccountName', 'debit', 'credit'];
dataSource: any;
Expand All @@ -35,13 +40,46 @@ export class SearchJournalEntryComponent implements OnInit {
},
{
option: 'System Entries',
value: false
value: false // Bug: unable to implement from server side
}
];
filterBy = [
{
type: 'officeId',
value: ''
},
{
type: 'glAccountId',
value: ''
},
{
type: 'manualEntriesOnly',
value: ''
},
{
type: 'transactionId',
value: ''
},
{
type: 'fromDate',
value: '1 July 2018'
},
{
type: 'toDate',
value: '31 July 2018'
},
{
type: 'dateFormat',
value: 'dd MMMM yyyy'
},
{
type: 'locale',
value: 'en'
}
];
filterValue: any = { officeName: '', glAccountCode: '', filter: '', transactionId: '' };

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatSort) sort: MatSort; // Bug: Unable to implement desc order sorting

constructor(private accountingService: AccountingService,
private router: Router) { }
Expand All @@ -52,33 +90,32 @@ export class SearchJournalEntryComponent implements OnInit {
this.getJournalEntries();
}

ngAfterViewInit() {
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
tap(() => this.loadJournalEntriesPage())
)
.subscribe();
}

getJournalEntries() {
this.accountingService.getJournalEntries().subscribe((journalEntryData: any) => {
this.dataSource = new MatTableDataSource(journalEntryData.pageItems);
this.dataSource.paginator = this.paginator;
this.dataSource.sortingDataAccessor = (transaction: any, property: any) => {
switch (property) {
case 'glAccountType': return transaction.glAccountType.value;
case 'debit': return transaction.amount;
case 'credit': return transaction.amount;
default: return transaction[property];
}
};
this.dataSource.sort = this.sort;
this.dataSource.filterPredicate = this.filterPredicate;
});
this.dataSource = new JournalEntriesDataSource(this.accountingService);
this.dataSource.loadJournalEntries(this.filterBy, this.sort.active, this.sort.direction, this.paginator.pageIndex, this.paginator.pageSize);
}

filterPredicate(data: any, filterValue: string) {
return data.officeName.indexOf(filterValue['officeName']) != -1
&& data.glAccountCode.indexOf(filterValue['glAccountCode']) != -1
&& data.manualEntry.toString().indexOf(filterValue['filter']) != -1
&& data.transactionId.indexOf(filterValue['transactionId']) != -1;
loadJournalEntriesPage() {
if(!this.sort.direction) {
delete this.sort.active;
}
this.dataSource.loadJournalEntries(this.filterBy, this.sort.active, this.sort.direction, this.paginator.pageIndex * this.paginator.pageSize, this.paginator.pageSize);
}

applyFilter(filterValue: string, property: string) {
this.filterValue[property] = filterValue;
this.dataSource.filter = this.filterValue;
this.paginator.pageIndex = 0;
let findIndex = this.filterBy.findIndex(filter => filter.type === property);
this.filterBy[findIndex].value = filterValue;
this.loadJournalEntriesPage();
}

getOffices() {
Expand Down

0 comments on commit bbbe772

Please sign in to comment.