Skip to content

Build your personal knowledge base with Trilium Notes

feat(table): basic add row button at the end

Elian Doran44fe6cde4ecf
diff --git a/apps/client/src/widgets/collections/table/index.css b/apps/client/src/widgets/collections/table/index.css
index c8dcc87936..b106ba3230 100644
--- a/apps/client/src/widgets/collections/table/index.css
+++ b/apps/client/src/widgets/collections/table/index.css
@@ -56,6 +56,25 @@
gap: 0.5em;
}
+/* The "new row" strip under the last row. Part of the scrolled content vertically, so it follows
+ the rows; pinned to the visible edge horizontally, so it reads whole at any scroll position.
+ The strip itself takes no clicks — only the button within it does, sitting past the "#" column
+ so that it lines up with the title a new row would get. */
+.table-view .tabulator-tableholder > .table-new-row-strip {
+ position: sticky;
+ inset-inline-start: 0;
+ width: 100%;
+ padding-inline-start: var(--row-handle-column-width, 0px);
+
+ > .table-new-row {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.4em;
+ padding: 5px 8px;
+ color: var(--muted-text-color);
+ }
+}
+
.tabulator button.tree-expand,
.tabulator button.tree-collapse {
display: inline-block;
diff --git a/apps/client/src/widgets/collections/table/index.tsx b/apps/client/src/widgets/collections/table/index.tsx
index b4c49faf70..1ac37dbf74 100644
--- a/apps/client/src/widgets/collections/table/index.tsx
+++ b/apps/client/src/widgets/collections/table/index.tsx
@@ -1,5 +1,6 @@
import "./index.css";
+import { createPortal } from "preact/compat";
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import { DataTreeModule, EditModule, FormatModule, FrozenColumnsModule, InteractionModule, MoveColumnsModule, MoveRowsModule, Options, PersistenceModule, ResizeColumnsModule, RowComponent,SortModule, Tabulator as VanillaTabulator} from 'tabulator-tables';
@@ -7,7 +8,7 @@ import { t } from "../../../services/i18n";
import SpacedUpdate from "../../../services/spaced_update";
import AttributeDetailWidget from "../../attribute_widgets/attribute_detail";
import CollectionProperties from "../../note_bars/CollectionProperties";
-import { ButtonOrActionButton } from "../../react/Button";
+import Button, { ButtonOrActionButton } from "../../react/Button";
import { useLegacyWidget } from "../../react/hooks";
import { ParentComponent } from "../../react/react_utils";
import { ViewModeProps } from "../interface";
@@ -21,6 +22,14 @@ import Tabulator from "./tabulator";
export default function TableView({ note, noteIds, viewConfig, saveConfig }: ViewModeProps<TableConfig>) {
const tabulatorRef = useRef<VanillaTabulator>(null);
const parentComponent = useContext(ParentComponent);
+ // The scrolled area under the rows, where the "new row" strip is portaled — reached for anew
+ // each time the table is built, as flipping into or out of a tree rebuilds it from scratch.
+ const [ rowsHolderEl, setRowsHolderEl ] = useState<Element | null>(null);
+ // The rendered width of the "#" column, which the strip's button sits past so that it lines up
+ // with the title column, the way the row it stands for would. Measured rather than derived: the
+ // width follows the row count's digits and the drag handles, both of which the columns are
+ // rebuilt over — and the child effect applying the new columns runs before this one measures.
+ const [ handleColWidth, setHandleColWidth ] = useState(0);
const [ attributeDetailWidgetEl, attributeDetailWidget ] = useLegacyWidget(() => new AttributeDetailWidget().contentSized());
const contextMenuEvents = useContextMenu(note, parentComponent, tabulatorRef);
@@ -46,6 +55,11 @@ export default function TableView({ note, noteIds, viewConfig, saveConfig }: Vie
row.getElement().classList.toggle("archived", !!data.isArchived);
}, []);
+ useEffect(() => {
+ if (!rowsHolderEl) return;
+ setHandleColWidth(tabulatorRef.current?.getColumns()[0]?.getWidth() ?? 0);
+ }, [ rowsHolderEl, columnDefs ]);
+
return (
<div className="table-view">
<CollectionProperties
@@ -76,8 +90,26 @@ export default function TableView({ note, noteIds, viewConfig, saveConfig }: Vie
movableColumns
movableRows={movableRows}
rowFormatter={rowFormatter}
+ onReady={() => setRowsHolderEl(
+ tabulatorRef.current?.element.querySelector(".tabulator-tableholder") ?? null
+ )}
{...dataTreeProps}
/>
+ {note.type !== "search" && rowsHolderEl && createPortal(
+ <div
+ className="table-new-row-strip"
+ style={{ "--row-handle-column-width": `${handleColWidth}px` }}
+ >
+ <Button
+ className="table-new-row"
+ kind="lowProfile"
+ icon="bx-plus"
+ text={t("table_view.new-row")}
+ triggerCommand="addNewRow"
+ />
+ </div>,
+ rowsHolderEl
+ )}
</>
)}
{attributeDetailWidgetEl}