cic-staff-client/src/app/pages/tokens/tokens.component.ts

57 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-11-25 09:00:20 +01:00
import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {TokenService} from '../../_services';
import {MatTableDataSource} from '@angular/material/table';
import {SelectionModel} from '@angular/cdk/collections';
import {Router} from '@angular/router';
@Component({
selector: 'app-tokens',
templateUrl: './tokens.component.html',
styleUrls: ['./tokens.component.scss']
})
export class TokensComponent implements OnInit, AfterViewInit {
dataSource: MatTableDataSource<any>;
columnsToDisplay = ['name', 'symbol', 'select'];
initialSelection = [];
allowMultiSelect = true;
selection: SelectionModel<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(
private tokenService: TokenService,
private router: Router
) { }
ngOnInit(): void {
this.dataSource = new MatTableDataSource(this.tokenService.data);
this.selection = new SelectionModel<any>(this.allowMultiSelect, this.initialSelection);
}
ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
isAllSelected(): boolean {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
masterToggle(): void {
this.isAllSelected() ? this.selection.clear() : this.dataSource.data.forEach(row => this.selection.select(row));
}
doFilter(value: string): void {
this.dataSource.filter = value.trim().toLocaleLowerCase();
}
viewToken(token): void {
this.router.navigateByUrl(`/tokens/${token.symbol}`).then();
}
}