From d7c2851008cd368dc731629f303e84f010922921 Mon Sep 17 00:00:00 2001 From: CentreMetre Date: Sun, 14 Jun 2026 17:28:57 +0100 Subject: [PATCH] Generalise abstract Table: rename generic to TRow, add docs Co-Authored-By: Claude Opus 4.8 (1M context) --- abstract-table.ts | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/abstract-table.ts b/abstract-table.ts index 962125d..bc5792e 100644 --- a/abstract-table.ts +++ b/abstract-table.ts @@ -43,6 +43,13 @@ export abstract class Table { this.messageEl = messageElement === undefined ? document.createElement("p") : messageElement; } + /** + * Creates the table headers from the provided example object and the headers orders and names if provided (otherwise inferred) + * @param exampleObject An example object of TRow, e.g. User = { id: 0, name: "Name"}. Used to store the types for each column. + * @param displayNamesAndOrder The order and names of the columns on the table. + * e.g. User = { fullname: "Full Name", id: "User ID" }, the headers would be "Full Name" and "User ID" in column order and headers, + * @return A array of {@link Header} type shape that holds the header/column info. + */ createHeadersFromType( exampleObject: TRow, // Needed for runtime, since types don't exist in JS. displayNamesAndOrder?: Record @@ -59,6 +66,9 @@ export abstract class Table { })); } + /** + * Sets the header values on the header row from the {@link headers} object on this object. + */ setHeaderRow() { const thead = document.createElement("thead"); const headerRow = document.createElement("tr"); @@ -73,13 +83,15 @@ export abstract class Table { this.table.appendChild(thead); } + /** - * Loads the CSS for tables into the html file for proper styling. + * Loads the CSS for tables into the html file for custom styling. + * @param relativePathAndName The path to go to for the cess file, including the filename and extension. */ - loadCSS() { + loadCSS(relativePathAndName: string) { const link = document.createElement("link"); link.rel = "stylesheet" - link.href = "../css/table.css"; // Relative path from JS folder. + link.href = relativePathAndName; link.type = 'text/css'; document.head.appendChild(link); } @@ -100,6 +112,10 @@ export abstract class Table { this.tableBody.appendChild(row) } + /** + * Appends multiple rows to the table, at the end off the table. + * @param data The rows to append to the table, in shape of {@link TRow}. + */ appendRows(data: TRow[]): void { if (data.length === 0) { this.setMessage("No data to append. Empty list provided.") @@ -177,20 +193,35 @@ export abstract class Table { this.appendRows(data) } - getMessageElement() { + /** + * Get the message element for the table. + * @return The HTMLElement thats used for showing messaged. Or null if its not set. + */ + getMessageElement(): HTMLElement | null { return this.messageEl; } + /** + * Clears the {@link messageEl}.textContent value to "". If the message element is not set it does nothing. + */ clearMessage() { if (!this.messageEl) return; this.messageEl.textContent = "" } + /** + * Appends a message to the alread existing table message, if the message element is set. Works even if current message is empty (""). + * @param message The message to append to the current message. + */ appendMessage(message: string) { if (!this.messageEl) return; this.messageEl.textContent = `${this.messageEl.textContent} ${message}` } + /** + * Sets the message, overwriting what it was before. Id the message element is not set, it does nothing. + * @param message The message to set the message element to. + */ setMessage(message: string) { if (!this.messageEl) return; this.clearMessage(); @@ -198,8 +229,8 @@ export abstract class Table { } } -type Header = { - key: keyof R; // The name of the field. +type Header = { + key: keyof TRow; // The name of the field. value: string; // The name of the header cell type: string; // The type of the field, can be string since typesof are returned as strings. } \ No newline at end of file