Files
smartguide/webapp/src/app/shared/ui/data-table/data-table.component.ts
T
Mats Paul Tončik 000b68758d
LocationBasedServices/smartguide/pipeline/head This commit looks good
Fixed navigation to single dfg project from person projects
2026-06-19 10:48:17 +02:00

117 lines
3.0 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';
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>('');
sortChange = output<Sort>();
displayedColumns = computed(() => this.columns().map((col) => col.id));
dataSource = new MatTableDataSource<T>([]);
sort = viewChild.required(MatSort);
constructor() {
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);
}
toggleRow(row: T): void {
if (!this.detailTemplate()) return;
this.expandedElement.update((current) => (current === row ? null : row));
}
}