94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormControl, 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 } from 'rxjs/operators';
|
|
|
|
import { ToCsvService } from '../../shared/to-csv.service';
|
|
import { Employee } from '../employee';
|
|
|
|
import { EmployeeListDataSource } from './employee-list-datasource';
|
|
|
|
@Component({
|
|
selector: 'app-employee-list',
|
|
templateUrl: './employee-list.component.html',
|
|
styleUrls: ['./employee-list.component.css'],
|
|
})
|
|
export class EmployeeListComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('filterElement', { static: true }) filterElement?: ElementRef;
|
|
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
|
|
@ViewChild(MatSort, { static: true }) sort?: MatSort;
|
|
dataSource: EmployeeListDataSource;
|
|
filter: Observable<string>;
|
|
form: FormGroup<{
|
|
filter: FormControl<string>;
|
|
}>;
|
|
|
|
list: Employee[] = [];
|
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
displayedColumns = [
|
|
'code',
|
|
'name',
|
|
'designation',
|
|
'salary',
|
|
'points',
|
|
'department',
|
|
'joiningDate',
|
|
'leavingDate',
|
|
];
|
|
|
|
constructor(private route: ActivatedRoute, private toCsv: ToCsvService) {
|
|
this.form = new FormGroup({
|
|
filter: new FormControl<string>('', { nonNullable: true }),
|
|
});
|
|
this.filter = this.form.controls.filter.valueChanges.pipe(
|
|
debounceTime(150),
|
|
distinctUntilChanged(),
|
|
);
|
|
this.dataSource = new EmployeeListDataSource(this.list, this.filter);
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { list: Employee[] };
|
|
|
|
this.list = data.list;
|
|
});
|
|
this.dataSource = new EmployeeListDataSource(this.list, this.filter, this.paginator, this.sort);
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.filterElement) {
|
|
this.filterElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
exportCsv() {
|
|
const headers = {
|
|
Code: 'code',
|
|
Name: 'name',
|
|
Designation: 'designation',
|
|
Salary: 'salary',
|
|
Points: 'points',
|
|
Department: 'department',
|
|
JoiningDate: 'joiningDate',
|
|
LeavingDate: 'leavingDate',
|
|
};
|
|
|
|
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {
|
|
type: 'text/csv;charset=utf-8;',
|
|
});
|
|
const link = document.createElement('a');
|
|
link.href = window.URL.createObjectURL(csvData);
|
|
link.setAttribute('download', 'employees.csv');
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
}
|
|
}
|