mirror of
https://github.com/zadam/trilium.git
synced 2025-11-04 21:49:04 +01:00
feat(book/table): support basic text columns
This commit is contained in:
parent
592e968f9f
commit
05aa087851
@ -28,14 +28,14 @@ export default class TableView extends ViewMode {
|
|||||||
|
|
||||||
private $root: JQuery<HTMLElement>;
|
private $root: JQuery<HTMLElement>;
|
||||||
private $container: JQuery<HTMLElement>;
|
private $container: JQuery<HTMLElement>;
|
||||||
private noteIds: string[];
|
private args: ViewModeArgs;
|
||||||
|
|
||||||
constructor(args: ViewModeArgs) {
|
constructor(args: ViewModeArgs) {
|
||||||
super(args);
|
super(args);
|
||||||
|
|
||||||
this.$root = $(TPL);
|
this.$root = $(TPL);
|
||||||
this.$container = this.$root.find(".table-view-container");
|
this.$container = this.$root.find(".table-view-container");
|
||||||
this.noteIds = args.noteIds;
|
this.args = args;
|
||||||
args.$parent.append(this.$root);
|
args.$parent.append(this.$root);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,10 +44,11 @@ export default class TableView extends ViewMode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async renderList() {
|
async renderList() {
|
||||||
const notes = await froca.getNotes(this.noteIds);
|
const { noteIds, parentNote } = this.args;
|
||||||
|
const notes = await froca.getNotes(noteIds);
|
||||||
|
|
||||||
this.$container.empty();
|
this.$container.empty();
|
||||||
renderTable(this.$container[0], notes);
|
renderTable(this.$container[0], parentNote, notes);
|
||||||
return this.$root;
|
return this.$root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,29 +1,63 @@
|
|||||||
import { GridOptions } from "ag-grid-community";
|
import { GridOptions } from "ag-grid-community";
|
||||||
import FNote from "../../../entities/fnote";
|
import FNote from "../../../entities/fnote";
|
||||||
|
|
||||||
interface Data {
|
type Data = {
|
||||||
title: string;
|
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"] {
|
export function buildColumnDefinitions(parentNote: FNote) {
|
||||||
return [
|
const columnDefs: GridOptions<Data>["columnDefs"] = [
|
||||||
{
|
{
|
||||||
field: "title"
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildRowDefinitions(notes: FNote[]): GridOptions<Data>["rowData"] {
|
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[], expectedLabels: string[]): GridOptions<Data>["rowData"] {
|
||||||
const definitions: GridOptions<Data>["rowData"] = [];
|
const definitions: GridOptions<Data>["rowData"] = [];
|
||||||
for (const note of notes) {
|
for (const note of notes) {
|
||||||
definitions.push(buildRowDefinition(note));
|
const data = {
|
||||||
|
title: note.title
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const expectedLabel of expectedLabels) {
|
||||||
|
data[expectedLabel] = note.getLabelValue(expectedLabel);
|
||||||
|
}
|
||||||
|
|
||||||
|
definitions.push(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return definitions;
|
return definitions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildRowDefinition(note: FNote): Data {
|
|
||||||
return {
|
|
||||||
title: note.title
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,16 +1,11 @@
|
|||||||
import { createGrid, AllCommunityModule, ModuleRegistry } from "ag-grid-community";
|
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";
|
import FNote from "../../../entities/fnote.js";
|
||||||
|
|
||||||
ModuleRegistry.registerModules([ AllCommunityModule ]);
|
ModuleRegistry.registerModules([ AllCommunityModule ]);
|
||||||
|
|
||||||
export default function renderTable(el: HTMLElement, notes: FNote[]) {
|
export default function renderTable(el: HTMLElement, parentNote: FNote, notes: FNote[]) {
|
||||||
const rowData = buildRowDefinitions(notes);
|
|
||||||
|
|
||||||
createGrid(el, {
|
createGrid(el, {
|
||||||
// Row Data: The data to be displayed.
|
...buildData(parentNote, notes)
|
||||||
rowData: rowData,
|
|
||||||
// Column Definitions: Defines the columns to be displayed.
|
|
||||||
columnDefs: buildColumnDefinitions()
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user