58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
import { MatPaginator } from '@angular/material/paginator';
|
|
import { MatSort } from '@angular/material/sort';
|
|
import { ActivatedRoute } from '@angular/router';
|
|
import { Observable } from 'rxjs';
|
|
import { debounceTime, distinctUntilChanged, startWith } from 'rxjs/operators';
|
|
|
|
import { Account } from '../../core/account';
|
|
|
|
import { AccountListDataSource } from './account-list-datasource';
|
|
|
|
@Component({
|
|
selector: 'app-account-list',
|
|
templateUrl: './account-list.component.html',
|
|
styleUrls: ['./account-list.component.css'],
|
|
})
|
|
export class AccountListComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('filterElement', { static: true }) filterElement?: ElementRef;
|
|
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
|
|
@ViewChild(MatSort, { static: true }) sort?: MatSort;
|
|
dataSource: AccountListDataSource;
|
|
filter: Observable<string>;
|
|
form: FormGroup;
|
|
list: Account[];
|
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
displayedColumns = ['name', 'type', 'isActive', 'isReconcilable', 'costCentre'];
|
|
|
|
constructor(private route: ActivatedRoute, private fb: FormBuilder) {
|
|
this.form = this.fb.group({
|
|
filter: '',
|
|
});
|
|
this.filter = this.listenToFilterChange();
|
|
}
|
|
|
|
listenToFilterChange() {
|
|
return this.form
|
|
.get('filter')
|
|
.valueChanges.pipe(startWith(''), debounceTime(150), distinctUntilChanged());
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { list: Account[] };
|
|
|
|
this.list = data.list;
|
|
});
|
|
this.dataSource = new AccountListDataSource(this.paginator, this.sort, this.filter, this.list);
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
this.filterElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
}
|