import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {MatPaginator, MatSort} from '@angular/material'; import {AccountListDataSource} from './account-list-datasource'; import {Account} from '../account'; import {ActivatedRoute} from '@angular/router'; import {Observable} from 'rxjs'; import {FormBuilder, FormGroup} from '@angular/forms'; import {debounceTime, distinctUntilChanged, startWith} from 'rxjs/operators'; @Component({ selector: 'app-account-list', templateUrl: './account-list.component.html', styleUrls: ['./account-list.component.css'] }) export class AccountListComponent implements OnInit, AfterViewInit { @ViewChild('filterElement') filterElement: ElementRef; @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; dataSource: AccountListDataSource; filter: Observable; 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.createForm(); this.filter = this.listenToFilterChange(); } createForm() { this.form = this.fb.group({ filter: '' }); } listenToFilterChange() { return this.form.get('filter').valueChanges .pipe( startWith(''), debounceTime(150), distinctUntilChanged() ); } ngOnInit() { this.route.data .subscribe((data: { 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); } }