Data table
Premium add-on. This page covers a premium add-on, included in Scriviamo Pro and Pro + Collaboration. See pricing
The data table add-on shows rows from a REST API that returns JSON, as a read-only table. It is a premium add-on.
<link rel="stylesheet" href="dist/addons/scriviamo.datatable.css">
import {DataTableNode, DataTablePaging} from "./dist/addons/scriviamo.datatable.js"
let orders = new DataTableNode()
orders.endpoint = "https://example.com/api/orders"
orders.rowsPath = "data.items"
orders.rowLimit = 20
orders.paging = DataTablePaging.pages
orders.dataColumns = [
{key: "id", title: "Order", type: "number"},
{key: "customer.name", title: "Customer"},
{key: "receipt", title: "Receipt", type: "link"}
]
editor.addNode(orders)
orders.load()
Users add one from the Insert menu; it loads straight away. They change its settings in the inspector and reload it from its toolbar.
Requests
load() requests endpoint and shows the rows in the response. It is the only thing that
sends a request: opening, undoing or sharing a document shows the rows saved with it.
| Property | Meaning |
|---|---|
endpoint | The address requested. It may be relative to the page. Only http and https are allowed. |
rowsPath | Where the array of rows is in the response, as a dotted path such as data.items. Empty when the response is the array itself. |
headers | Request headers, as {name: value}. They are saved with the document. |
includeCredentials | Sends the browser's cookies, even to another site |
While a request is in progress the rows shown so far stay, with a loading indicator under
the table. A request that fails keeps them too, and shows why under the table with a Retry
button. isLoading and lastError report the same, and cancel() stops a request.
Signing requests
Headers in headers travel with the document to everyone who opens it, so never put a
password or token there. Add them as each request is sent instead, with
DataTableNode.prepareRequest:
DataTableNode.prepareRequest = async request => {
request.headers.Authorization = `Bearer ${await getAccessToken()}`
return request
}
The request has the node, the url, the headers and the credentials mode. Change them
and return the request, or return nothing to send it unchanged.
Columns
With no columns set, every key in the rows becomes a column, in the order the keys first appear:
- a column whose values are all numbers is right-aligned;
- a key listed in
imageKeys(["thumbnailUrl"]by default) is drawn as images; - rows that are arrays get numbered columns: Column 1, Column 2 and so on.
To choose the columns, set dataColumns. Each has a key, an optional title and an
optional type:
| Type | Shows |
|---|---|
text | The value as text; objects as JSON. The default. |
number | Text, right-aligned with its heading |
image | An image from the value's address |
link | A link to the value's address, opening in a new window |
A key can reach into nested values with dots: customer.name, or items.0.sku.
Only http and https addresses, and data: image URIs, are drawn as images. Only http,
https and mailto addresses are drawn as links. Anything else is shown as text, so a row
can't run a script through an address.
In the inspector, columns are written one per line as key | title | type, which
dataColumnsText reads and writes.
Paging
rowLimit is the number of rows on a page. paging says how pages are requested:
DataTablePaging | Requests |
|---|---|
none | One request, limited to rowLimit rows. The default. |
pages | A page at a time, with the page number in pageParameter, counting from firstPage |
offset | A page at a time, with the first row's offset in pageParameter |
local | Every row at once; the table shows a page at a time from them |
limitParameter (_limit by default) carries rowLimit; set it to "" to send none.
pageParameter is _page by default.
With paging, Previous and Next buttons and the page number appear under the table. When the
response has an X-Total-Count header, the table also shows how many pages there are;
without one, a full page means there may be another. In code, use page, pageCount,
hasNextPage, hasPreviousPage, nextPage(), previousPage() and showPage(page).
Rows and changes
data is the rows received. Assigning it shows those rows without a request, and is a change
to the document, with an undo step. Rows a request returns, and moving between pages, only
redraw the table: they don't mark the document changed.
Events
The editor raises these as the table's requests finish:
| Event | detail | When |
|---|---|---|
dataTableLoaded | {node, rows} | A request's rows are shown |
dataTableFailed | {node, message} | A request failed; the rows shown before stay |
Export
HTML export writes an ordinary table, with image columns as <img> and link columns as links.
Markdown and plain text write each address as text.
Saved settings
The JSON for a data table has the table's cells and data, plus endpoint, rowLimit,
rowsPath, dataColumns, paging, pageParameter, firstPage, page, limitParameter,
headers, includeCredentials and imageKeys when they differ from the defaults.