Added sort query param manipulation by generic data table
This commit is contained in:
@@ -15,17 +15,15 @@
|
||||
<app-data-table
|
||||
[data]="euProjectData"
|
||||
[columns]="euProjectColumns"
|
||||
[activeSortKey]="urlSortKey()"
|
||||
[sortDirection]="urlSortDirection()"
|
||||
(sortChange)="handleSortChange($event)" />
|
||||
queryParamSortKey="sort_by"
|
||||
queryParamSortDirectionKey="sort_direction" />
|
||||
</mat-tab>
|
||||
<mat-tab label="Bund">
|
||||
<app-data-table
|
||||
[data]="bundProjectData"
|
||||
[columns]="bundProjectColumns"
|
||||
[activeSortKey]="urlSortKey()"
|
||||
[sortDirection]="urlSortDirection()"
|
||||
(sortChange)="handleSortChange($event)" />
|
||||
queryParamSortKey="sort_by"
|
||||
queryParamSortDirectionKey="sort_direction" />
|
||||
</mat-tab>
|
||||
</mat-tab-group>
|
||||
<app-query-param-paginator
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
effect,
|
||||
input,
|
||||
OnInit,
|
||||
signal,
|
||||
TemplateRef,
|
||||
viewChild,
|
||||
} from '@angular/core';
|
||||
@@ -26,8 +25,7 @@ import {
|
||||
DataTableComponent,
|
||||
} from '../../shared/ui/data-table/data-table.component';
|
||||
import { CurrencyPipe, DatePipe } from '@angular/common';
|
||||
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
|
||||
import { Sort, SortDirection } from '@angular/material/sort';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { InstitutionProjectsFundingGraphComponent } from '../institution-projects-funding-graph/institution-projects-funding-graph.component';
|
||||
@@ -117,22 +115,10 @@ export class InstitutionProjectsComponent implements OnInit {
|
||||
return funding.annual_average_over_period ?? 0;
|
||||
});
|
||||
|
||||
urlSortKey = signal<string>('');
|
||||
urlSortDirection = signal<SortDirection>('');
|
||||
|
||||
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',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,15 @@
|
||||
[columns]="dfgProjectColumns"
|
||||
[data]="dfgProjectData"
|
||||
[detailTemplate]="dfgProjectDetailRowTmp"
|
||||
[activeSortKey]="urlSortKey()"
|
||||
[sortDirection]="urlSortDirection()"
|
||||
(sortChange)="handleSortChange($event)" />
|
||||
queryParamSortKey="sort_by"
|
||||
queryParamSortDirectionKey="sort_direction" />
|
||||
</mat-tab>
|
||||
<mat-tab label="EU">
|
||||
<app-data-table
|
||||
[columns]="personCordisEuProjectColumns"
|
||||
[data]="personCordisEuProjectData" />
|
||||
[data]="personCordisEuProjectData"
|
||||
queryParamSortKey="sort_by"
|
||||
queryParamSortDirectionKey="sort_direction" />
|
||||
</mat-tab>
|
||||
</mat-tab-group>
|
||||
<app-query-param-paginator
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
effect,
|
||||
input,
|
||||
OnInit,
|
||||
signal,
|
||||
TemplateRef,
|
||||
viewChild,
|
||||
} from '@angular/core';
|
||||
@@ -24,8 +23,7 @@ import { MatCardModule } from '@angular/material/card';
|
||||
import { MatTabsModule } from '@angular/material/tabs';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
|
||||
import { Sort, SortDirection } from '@angular/material/sort';
|
||||
import { Router, RouterModule } from '@angular/router';
|
||||
import { CurrencyPipe, DatePipe } from '@angular/common';
|
||||
import { QueryParamPaginatorComponent } from '../../shared/ui/query-param-paginator/query-param-paginator.component';
|
||||
import { QueryParamInputComponent } from '../../shared/ui/query-param-input/query-param-input.component';
|
||||
@@ -81,17 +79,11 @@ export class PersonProjectsComponent implements OnInit {
|
||||
'euProjectsActionsTmp',
|
||||
);
|
||||
|
||||
urlSortKey = signal<string>('');
|
||||
urlSortDirection = signal<SortDirection>('');
|
||||
|
||||
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',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
mat-table
|
||||
[dataSource]="dataSource"
|
||||
matSort
|
||||
(matSortChange)="onSortChange($event)"
|
||||
(matSortChange)="_handleSortChange($event)"
|
||||
multiTemplateDataRows>
|
||||
@for (col of columns(); track col.id) {
|
||||
<ng-container [matColumnDef]="col.id">
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -28,11 +28,11 @@ export class QueryParamInputComponent {
|
||||
debounceTime = input<number>(300);
|
||||
handleValueChange = output<string>();
|
||||
|
||||
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<string>(() => {
|
||||
|
||||
Reference in New Issue
Block a user