diff --git a/webapp/src/app/modules/institution-projects/institution-projects.component.html b/webapp/src/app/modules/institution-projects/institution-projects.component.html index 8d0b6d47..6ad4c8f9 100644 --- a/webapp/src/app/modules/institution-projects/institution-projects.component.html +++ b/webapp/src/app/modules/institution-projects/institution-projects.component.html @@ -15,17 +15,15 @@ + queryParamSortKey="sort_by" + queryParamSortDirectionKey="sort_direction" /> + queryParamSortKey="sort_by" + queryParamSortDirectionKey="sort_direction" /> (''); - urlSortDirection = signal(''); - constructor( private readonly activatedRoute: ActivatedRoute, private readonly router: Router, ) { - this.activatedRoute.queryParams - .pipe(takeUntilDestroyed()) - .subscribe((params) => { - this.urlSortKey.set(params['sort_by'] ?? ''); - this.urlSortDirection.set( - (params['sort_direction'] as SortDirection) ?? '', - ); - }); - effect(() => { const euPage = this.cordisEuProjectPage(); if (euPage) { @@ -289,11 +275,4 @@ export class InstitutionProjectsComponent implements OnInit { queryParamsHandling: 'merge', }); } - - handleSortChange(event: Sort): void { - this.router.navigate([], { - queryParams: { sort_by: event.active, sort_direction: event.direction }, - queryParamsHandling: 'merge', - }); - } } diff --git a/webapp/src/app/modules/person-projects/person-projects.component.html b/webapp/src/app/modules/person-projects/person-projects.component.html index 7faf7c59..ed14000b 100644 --- a/webapp/src/app/modules/person-projects/person-projects.component.html +++ b/webapp/src/app/modules/person-projects/person-projects.component.html @@ -12,14 +12,15 @@ [columns]="dfgProjectColumns" [data]="dfgProjectData" [detailTemplate]="dfgProjectDetailRowTmp" - [activeSortKey]="urlSortKey()" - [sortDirection]="urlSortDirection()" - (sortChange)="handleSortChange($event)" /> + queryParamSortKey="sort_by" + queryParamSortDirectionKey="sort_direction" /> + [data]="personCordisEuProjectData" + queryParamSortKey="sort_by" + queryParamSortDirectionKey="sort_direction" /> (''); - urlSortDirection = signal(''); - readonly pageTotal = computed(() => { return this.dfgProjectPage()?.total ?? 0; }); - constructor( - private readonly router: Router, - private readonly activatedRoute: ActivatedRoute, - ) { + constructor(private readonly router: Router) { effect(() => { this.dfgProjectData = this.dfgProjectPage().elements || []; }); @@ -165,11 +157,4 @@ export class PersonProjectsComponent implements OnInit { }, ]; } - - handleSortChange(event: Sort): void { - this.router.navigate([], { - queryParams: { sort_by: event.active, sort_direction: event.direction }, - queryParamsHandling: 'merge', - }); - } } diff --git a/webapp/src/app/shared/ui/data-table/data-table.component.html b/webapp/src/app/shared/ui/data-table/data-table.component.html index 41347477..babc2916 100644 --- a/webapp/src/app/shared/ui/data-table/data-table.component.html +++ b/webapp/src/app/shared/ui/data-table/data-table.component.html @@ -3,7 +3,7 @@ mat-table [dataSource]="dataSource" matSort - (matSortChange)="onSortChange($event)" + (matSortChange)="_handleSortChange($event)" multiTemplateDataRows> @for (col of columns(); track col.id) { diff --git a/webapp/src/app/shared/ui/data-table/data-table.component.ts b/webapp/src/app/shared/ui/data-table/data-table.component.ts index 0e4c9d16..6fa71428 100644 --- a/webapp/src/app/shared/ui/data-table/data-table.component.ts +++ b/webapp/src/app/shared/ui/data-table/data-table.component.ts @@ -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 { id: string; @@ -55,15 +58,37 @@ export class DataTableComponent implements AfterViewInit { containerOverflow = computed(() => (this.maxHeight() ? 'auto' : 'visible')); stickyHeader = input(true); - activeSortKey = input(''); - sortDirection = input(''); - sortChange = output(); + queryParamSortKey = input(null); + queryParamSortDirectionKey = input(null); + + _urlSortValue = computed(() => { + const params = this.queryParams(); + const key = this.queryParamSortKey(); + + return params && key ? params[key] || null : null; + }); + + _urlSortDirectionValue = computed(() => { + const params = this.queryParams(); + const key = this.queryParamSortDirectionKey(); + + return params && key ? params[key] || null : null; + }); + + handleSortChange = output(); displayedColumns = computed(() => this.columns().map((col) => col.id)); dataSource = new MatTableDataSource([]); 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 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 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 { diff --git a/webapp/src/app/shared/ui/query-param-input/query-param-input.component.ts b/webapp/src/app/shared/ui/query-param-input/query-param-input.component.ts index 85d396f1..3a3f62a7 100644 --- a/webapp/src/app/shared/ui/query-param-input/query-param-input.component.ts +++ b/webapp/src/app/shared/ui/query-param-input/query-param-input.component.ts @@ -28,11 +28,11 @@ export class QueryParamInputComponent { debounceTime = input(300); handleValueChange = output(); - private readonly activatedRouter = inject(ActivatedRoute); + private readonly activatedRoute = inject(ActivatedRoute); private readonly router = inject(Router); private readonly queryParams = toSignal( - this.activatedRouter.queryParams.pipe(takeUntilDestroyed()), + this.activatedRoute.queryParams.pipe(takeUntilDestroyed()), ); _urlValue = computed(() => {