130 lines
3.4 KiB
TypeScript
130 lines
3.4 KiB
TypeScript
import { NgTemplateOutlet } from '@angular/common';
|
|
import {
|
|
AfterViewInit,
|
|
Component,
|
|
computed,
|
|
effect,
|
|
input,
|
|
output,
|
|
signal,
|
|
TemplateRef,
|
|
viewChild,
|
|
} from '@angular/core';
|
|
import {
|
|
MatSort,
|
|
MatSortModule,
|
|
Sort,
|
|
SortDirection,
|
|
} from '@angular/material/sort';
|
|
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
|
|
import { Router } from '@angular/router';
|
|
|
|
export interface BaseColumn<T> {
|
|
id: string;
|
|
header: string;
|
|
type: 'accessor' | 'display' | 'group';
|
|
cellTemplate?: TemplateRef<any>;
|
|
}
|
|
|
|
export interface AccessorColumn<T> extends BaseColumn<T> {
|
|
type: 'accessor';
|
|
accessorKey: keyof T;
|
|
}
|
|
|
|
export interface DisplayColumn<T> extends BaseColumn<T> {
|
|
type: 'display';
|
|
}
|
|
|
|
export type GenericColumn<T> = AccessorColumn<T> | DisplayColumn<T>;
|
|
|
|
@Component({
|
|
selector: 'app-data-table',
|
|
standalone: true,
|
|
imports: [MatTableModule, NgTemplateOutlet, MatSortModule],
|
|
templateUrl: './data-table.component.html',
|
|
styleUrl: './data-table.component.scss',
|
|
})
|
|
export class DataTableComponent<T> implements AfterViewInit {
|
|
data = input.required<T[]>();
|
|
columns = input.required<GenericColumn<T>[]>();
|
|
filterTerm = input<string>('');
|
|
|
|
detailTemplate = input<TemplateRef<any> | null>(null);
|
|
expandedElement = signal<T | null>(null);
|
|
|
|
maxHeight = input<string | null>(null);
|
|
containerOverflow = computed(() => (this.maxHeight() ? 'auto' : 'visible'));
|
|
stickyHeader = input<boolean>(true);
|
|
|
|
activeSortKey = input<string>('');
|
|
sortDirection = input<SortDirection>('');
|
|
navigatePrefix=input<string>('');
|
|
navigateField=input<string>('');
|
|
sortChange = output<Sort>();
|
|
|
|
displayedColumns = computed(() => this.columns().map((col) => col.id));
|
|
dataSource = new MatTableDataSource<T>([]);
|
|
|
|
sort = viewChild.required(MatSort);
|
|
|
|
constructor(private router:Router) {
|
|
effect(() => {
|
|
const currentData = this.data();
|
|
|
|
this.dataSource.data = currentData;
|
|
|
|
this.dataSource.filterPredicate = (data: T, filter: string) => {
|
|
const columnsToSearch = this.columns()
|
|
.filter((col): col is AccessorColumn<T> => col.type === 'accessor')
|
|
.map((col) => col.accessorKey);
|
|
|
|
return columnsToSearch.some((key) => {
|
|
const value = data[key];
|
|
return value ? String(value).toLowerCase().includes(filter) : false;
|
|
});
|
|
};
|
|
|
|
this.dataSource.filter = this.filterTerm().trim().toLowerCase();
|
|
});
|
|
|
|
effect(() => {
|
|
const matSortInstance = this.sort();
|
|
const activeKey = this.activeSortKey();
|
|
const direction = this.sortDirection();
|
|
|
|
if (
|
|
matSortInstance.active !== activeKey ||
|
|
matSortInstance.direction !== direction
|
|
) {
|
|
matSortInstance.active = activeKey;
|
|
matSortInstance.direction = direction;
|
|
|
|
this.dataSource.sort = matSortInstance;
|
|
}
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit(): void {
|
|
this.dataSource.sort = this.sort();
|
|
}
|
|
|
|
onSortChange(sort: Sort) {
|
|
this.sortChange.emit(sort);
|
|
}
|
|
|
|
navigateToDataField(row: any): void {
|
|
if (!this.navigatePrefix() || !this.navigateField()) {
|
|
return;
|
|
}
|
|
const value = row[this.navigateField()];
|
|
if (value != null) {
|
|
this.router.navigate([this.navigatePrefix(), value]);
|
|
}
|
|
}
|
|
|
|
toggleRow(row: T): void {
|
|
if (!this.detailTemplate()) return;
|
|
this.expandedElement.update((current) => (current === row ? null : row));
|
|
}
|
|
}
|