Added search for institution bund and eu projects
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import { NgTemplateOutlet } from '@angular/common';
|
||||
import {
|
||||
AfterViewInit,
|
||||
Component,
|
||||
computed,
|
||||
effect,
|
||||
input,
|
||||
output,
|
||||
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>[]>();
|
||||
|
||||
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;
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user