Skip to content

Build your personal knowledge base with Trilium Notes

feat(book/table): support basic text columns

Elian Doran05aa08785152
diff --git a/apps/client/src/widgets/view_widgets/table_view.ts b/apps/client/src/widgets/view_widgets/table_view.ts
index b87b727022..0505f09c7d 100644
--- a/apps/client/src/widgets/view_widgets/table_view.ts
+++ b/apps/client/src/widgets/view_widgets/table_view.ts
@@ -28,14 +28,14 @@ export default class TableView extends ViewMode {
private $root: JQuery<HTMLElement>;
private $container: JQuery<HTMLElement>;
- private noteIds: string[];
+ private args: ViewModeArgs;
constructor(args: ViewModeArgs) {
super(args);
this.$root = $(TPL);
this.$container = this.$root.find(".table-view-container");
- this.noteIds = args.noteIds;
+ this.args = args;
args.$parent.append(this.$root);
}
@@ -44,10 +44,11 @@ export default class TableView extends ViewMode {
}
async renderList() {
- const notes = await froca.getNotes(this.noteIds);
+ const { noteIds, parentNote } = this.args;
+ const notes = await froca.getNotes(noteIds);
this.$container.empty();
- renderTable(this.$container[0], notes);
+ renderTable(this.$container[0], parentNote, notes);
return this.$root;
}
diff --git a/apps/client/src/widgets/view_widgets/table_view/data.ts b/apps/client/src/widgets/view_widgets/table_view/data.ts
index a9b164cc0c..be6a968e6c 100644
--- a/apps/client/src/widgets/view_widgets/table_view/data.ts
+++ b/apps/client/src/widgets/view_widgets/table_view/data.ts
@@ -1,29 +1,63 @@
import { GridOptions } from "ag-grid-community";
import FNote from "../../../entities/fnote";
-interface Data {
+type Data = {
title: string;
+} & Record<string, string>;
+
+export function buildData(parentNote: FNote, notes: FNote[]) {
+ const { columnDefs, expectedLabels } = buildColumnDefinitions(parentNote);
+ const rowData = buildRowDefinitions(notes, expectedLabels);
+
+ return {
+ rowData,
+ columnDefs
+ }
}
-export function buildColumnDefinitions(): GridOptions<Data>["columnDefs"] {
- return [
+export function buildColumnDefinitions(parentNote: FNote) {
+ const columnDefs: GridOptions<Data>["columnDefs"] = [
{
field: "title"
}
];
+
+ const expectedLabels: string[] = [];
+
+ for (const promotedAttribute of parentNote.getPromotedDefinitionAttributes()) {
+ console.log(promotedAttribute);
+ if (promotedAttribute.type !== "label") {
+ console.warn("Relations are not supported for now");
+ continue;
+ }
+
+ const def = promotedAttribute.getDefinition();
+ const attributeName = promotedAttribute.name.split(":", 2)[1];
+ const title = def.promotedAlias ?? attributeName;
+
+ columnDefs.push({
+ field: attributeName,
+ headerName: title
+ });
+ expectedLabels.push(attributeName);
+ }
+
+ return { columnDefs, expectedLabels };
}
-export function buildRowDefinitions(notes: FNote[]): GridOptions<Data>["rowData"] {
+export function buildRowDefinitions(notes: FNote[], expectedLabels: string[]): GridOptions<Data>["rowData"] {
const definitions: GridOptions<Data>["rowData"] = [];
for (const note of notes) {
- definitions.push(buildRowDefinition(note));
- }
+ const data = {
+ title: note.title
+ };
- return definitions;
-}
+ for (const expectedLabel of expectedLabels) {
+ data[expectedLabel] = note.getLabelValue(expectedLabel);
+ }
-export function buildRowDefinition(note: FNote): Data {
- return {
- title: note.title
+ definitions.push(data);
}
+
+ return definitions;
}
diff --git a/apps/client/src/widgets/view_widgets/table_view/renderer.ts b/apps/client/src/widgets/view_widgets/table_view/renderer.ts
index 385acd1bfe..b8e9b92ca5 100644
--- a/apps/client/src/widgets/view_widgets/table_view/renderer.ts
+++ b/apps/client/src/widgets/view_widgets/table_view/renderer.ts
@@ -1,16 +1,11 @@
import { createGrid, AllCommunityModule, ModuleRegistry } from "ag-grid-community";
-import { buildColumnDefinitions, buildRowDefinitions } from "./data.js";
+import { buildData } from "./data.js";
import FNote from "../../../entities/fnote.js";
ModuleRegistry.registerModules([ AllCommunityModule ]);
-export default function renderTable(el: HTMLElement, notes: FNote[]) {
- const rowData = buildRowDefinitions(notes);
-
+export default function renderTable(el: HTMLElement, parentNote: FNote, notes: FNote[]) {
createGrid(el, {
- // Row Data: The data to be displayed.
- rowData: rowData,
- // Column Definitions: Defines the columns to be displayed.
- columnDefs: buildColumnDefinitions()
+ ...buildData(parentNote, notes)
});
}