Added sort query param manipulation by generic data table

This commit is contained in:
Mats Paul Tončik
2026-06-22 15:48:57 +02:00
parent c222d422a9
commit 043db9b7cc
7 changed files with 74 additions and 61 deletions
@@ -4,12 +4,14 @@ import {
Component,
computed,
effect,
inject,
input,
output,
signal,
TemplateRef,
viewChild,
} from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import {
MatSort,
MatSortModule,
@@ -17,6 +19,7 @@ import {
SortDirection,
} from '@angular/material/sort';
import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
export interface BaseColumn<T> {
id: string;
@@ -55,15 +58,37 @@ export class DataTableComponent<T> implements AfterViewInit {
containerOverflow = computed(() => (this.maxHeight() ? 'auto' : 'visible'));
stickyHeader = input<boolean>(true);
activeSortKey = input<string>('');
sortDirection = input<SortDirection>('');
sortChange = output<Sort>();
queryParamSortKey = input<string | null>(null);
queryParamSortDirectionKey = input<string | null>(null);
_urlSortValue = computed<string | null>(() => {
const params = this.queryParams();
const key = this.queryParamSortKey();
return params && key ? params[key] || null : null;
});
_urlSortDirectionValue = computed<string | null>(() => {
const params = this.queryParams();
const key = this.queryParamSortDirectionKey();
return params && key ? params[key] || null : null;
});
handleSortChange = output<Sort>();
displayedColumns = computed(() => this.columns().map((col) => col.id));
dataSource = new MatTableDataSource<T>([]);
sort = viewChild.required(MatSort);
private readonly activatedRouter = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly queryParams = toSignal(
this.activatedRouter.queryParams.pipe(takeUntilDestroyed()),
);
constructor() {
effect(() => {
const currentData = this.data();
@@ -86,15 +111,23 @@ export class DataTableComponent<T> implements AfterViewInit {
effect(() => {
const matSortInstance = this.sort();
const activeKey = this.activeSortKey();
const direction = this.sortDirection();
const activeKey = this._urlSortValue();
const direction = this._urlSortDirectionValue();
if (
matSortInstance.active !== activeKey ||
matSortInstance.direction !== direction
) {
matSortInstance.active = activeKey;
matSortInstance.direction = direction;
if (activeKey == null) {
matSortInstance.active = '';
} else {
matSortInstance.active = activeKey;
}
if (direction == null) {
matSortInstance.direction = '';
} else {
matSortInstance.direction = direction as SortDirection;
}
this.dataSource.sort = matSortInstance;
}
@@ -105,8 +138,25 @@ export class DataTableComponent<T> implements AfterViewInit {
this.dataSource.sort = this.sort();
}
onSortChange(sort: Sort) {
this.sortChange.emit(sort);
_handleSortChange(sort: Sort): void {
this.handleSortChange.emit(sort);
const sortKey = this.queryParamSortKey();
const sortDirectionKey = this.queryParamSortDirectionKey();
if (sortKey != null) {
const queryParams: any = {
[sortKey]: sort.active || null,
};
if (sortDirectionKey != null) {
queryParams[sortDirectionKey] = sort.direction || null;
}
this.router.navigate([], {
queryParams,
queryParamsHandling: 'merge',
});
}
}
toggleRow(row: T): void {