-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/table component #2786
base: master
Are you sure you want to change the base?
Feat/table component #2786
Changes from all commits
19c1196
d30b646
8b23d89
787ae12
56161fb
68621c6
1a0256b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="table-container"> | ||
<table mat-table [dataSource]="dataSource" matSort> | ||
<ng-container *ngFor="let column of columnsToDisplay" [matColumnDef]="column"> | ||
<th mat-header-cell *matHeaderCellDef mat-sort-header>{{ column }}</th> | ||
<td mat-cell *matCellDef="let element">{{ element[column] }}</td> | ||
</ng-container> | ||
|
||
<tr mat-header-row *matHeaderRowDef="columnsToDisplay; sticky: true"></tr> | ||
<tr mat-row *matRowDef="let row; columns: columnsToDisplay"></tr> | ||
</table> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.table-container { | ||
width: 100%; | ||
max-height: 80vh; | ||
overflow: auto; | ||
|
||
tr[mat-header-row] { | ||
background-color: var(--ion-background-color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectorRef, | ||
Component, | ||
computed, | ||
effect, | ||
OnInit, | ||
ViewChild, | ||
} from "@angular/core"; | ||
import { TemplateBaseComponent } from "../base"; | ||
import { FlowTypes } from "packages/data-models"; | ||
import { getBooleanParamFromTemplateRow } from "src/app/shared/utils"; | ||
import { AppDataService } from "src/app/shared/services/data/app-data.service"; | ||
import { MatTableDataSource } from "@angular/material/table"; | ||
import { MatSort } from "@angular/material/sort"; | ||
|
||
interface ITableParams { | ||
/** TEMPLATE PARAMETER: show_id. Display the data lists's ID column in the table. Default false */ | ||
showId: boolean; | ||
} | ||
|
||
@Component({ | ||
selector: "plh-table", | ||
templateUrl: "./table.component.html", | ||
styleUrls: ["./table.component.scss"], | ||
}) | ||
export class TmplTableComponent extends TemplateBaseComponent implements AfterViewInit { | ||
params = computed(() => this.getParams(this.parameterList())); | ||
dataRows = computed(() => this.getDataRowsFromValue(this.value())); | ||
|
||
columnsToDisplay = []; | ||
dataSource: MatTableDataSource<any>; | ||
|
||
@ViewChild(MatSort) sort: MatSort; | ||
|
||
constructor( | ||
private appDataService: AppDataService, | ||
private cdr: ChangeDetectorRef | ||
) { | ||
super(); | ||
effect(async () => { | ||
const data = (await this.dataRows()) || []; | ||
this.dataSource = new MatTableDataSource(data); | ||
this.columnsToDisplay = this.getColumnsToDisplayFromData(data); | ||
if (this.sort) { | ||
this.dataSource.sort = this.sort; | ||
} | ||
this.cdr.detectChanges(); | ||
}); | ||
} | ||
|
||
ngAfterViewInit() { | ||
if (this.dataSource) { | ||
this.dataSource.sort = this.sort; | ||
} | ||
} | ||
|
||
private getParams(authorParams: FlowTypes.TemplateRow["parameter_list"]): ITableParams { | ||
return { | ||
showId: getBooleanParamFromTemplateRow(this._row, "show_id", false), | ||
}; | ||
} | ||
|
||
private async getDataRowsFromValue(dataListName: string) { | ||
// TODO: handle data passed as @data format, and query dynamic data to return live data (see data-items handling) | ||
if (typeof dataListName !== "string") { | ||
console.log("[TABLE COMPONENT] Value must be a data list name string"); | ||
return []; | ||
} | ||
const dataList = await this.appDataService.getSheet("data_list", dataListName); | ||
return dataList?.rows; | ||
} | ||
|
||
private getColumnsToDisplayFromData(data: FlowTypes.Data_listRow[]): string[] { | ||
if (!data || !data.length) return []; | ||
// Infer columns from first data row | ||
const columnNames = Object.keys(data[0]); | ||
return this.params().showId | ||
? columnNames | ||
: columnNames.filter((columnName) => columnName !== "id"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import { NouisliderModule } from "ng2-nouislider"; | |
import { RouterModule } from "@angular/router"; | ||
import { SwiperModule } from "swiper/angular"; | ||
import { NgxExtendedPdfViewerModule } from "ngx-extended-pdf-viewer"; | ||
import { MatTableModule } from "@angular/material/table"; | ||
import { MatSortModule } from "@angular/material/sort"; | ||
|
||
import { SharedPipesModule } from "../../pipes"; | ||
import { TooltipDirective } from "../common/directives/tooltip.directive"; | ||
|
@@ -26,14 +28,16 @@ import { DEMO_COMPONENTS } from "packages/components/demo"; | |
imports: [ | ||
CommonModule, | ||
FormsModule, | ||
ReactiveFormsModule, | ||
IonicModule, | ||
SharedPipesModule, | ||
NouisliderModule, | ||
LottieModule, | ||
MatSortModule, | ||
MatTableModule, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit(non-blocking): I can't remember how well we've implemented optimisation for this module, but might be more viable to include the imports in the table component itself and turn into a standalone component so that the modules are easier to remove from build when the component is not needed |
||
NgxExtendedPdfViewerModule, | ||
NouisliderModule, | ||
ReactiveFormsModule, | ||
RouterModule, | ||
SharedPipesModule, | ||
SwiperModule, | ||
NgxExtendedPdfViewerModule, | ||
TemplatePipesModule, | ||
], | ||
exports: [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit(blocking): the
getDataRows
from value function is async, so this computed will not behave as expected (no such thing as async computed). Instead it would be better to use an effect to respond to value changes and set the dataRows as a signal. I think it still kinda works in the frontend due to the additional cdr calls, but I'd consider that to be more of a bug than expected behaviour