Declare and map columns
Name the columns a widget needs in its declaration, let the person map their own columns to them, then read and write under your names.
A widget doesn't know the person's column names. It declares its own, and
the person maps a column of their table to each in Grist's custom widget
panel: their Task name to your Title, say. Your code only ever uses
your names.
Declare the columns
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "full",
columns: [
{ name: "Title", type: "Text" },
{ name: "Done", type: "Bool" },
{ name: "Tags", type: "ChoiceList", optional: true, allowMultiple: true, description: "Columns of tags to show." },
],
})| Field | |
|---|---|
name | Required. The name your code uses, a valid identifier. |
type | The Grist types the column may have, comma-separated ("Text,Choice"). Defaults to any type. It types what you read (Typed reads). |
title, description | The label and the help text in Grist's mapping panel. |
optional | true lets the widget render while it isn't mapped. Required by default. |
allowMultiple | true lets the person map several columns to this name: you read their values as a list. |
strictType | true makes type exact: "Any" then matches only an Any column. |
Plain strings work too (columns: ["Title", "Done"]), with no type.
What the person sees
Until every required column is mapped, a widget mounted with
mountGristWidget() shows "Map your columns" with the missing names; it
renders once they are mapped. Mappings can change at any time: the widget
re-renders with the new ones, nothing to reload. The declared columns
themselves are read once, when the widget starts.
Read under your names
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "read table",
columns: [
{ name: "Title", type: "Text" },
{ name: "Done", type: "Bool" },
],
})
export default function App() {
const w = widget.useGrist()
if (!w.mappedRecord) return <p>Select a task.</p>
return (
<p>
{w.mappedRecord.Title} {w.mappedRecord.Done ? "(done)" : ""}
</p>
)
}w.mappedRecordis the selected row under your names, typed from the declaration, ornullwhile a required column is unmapped or no row is selected.w.mappedRecordsis every row of the section the same way.w.mappingssays which column each name is mapped to ({ Title: "Task_name" }), a list for anallowMultiplename.w.columnMappingStatussays whether the widget can render:ok, and themissingnames.mountGristWidget()already waits on it; a component that renders outside its boundary checks it.
The other columns
Once the person maps a column, Grist sends the widget only the mapped
columns and the row id, in w.mappedRecord, w.mappedRecords and the
raw w.record. A widget that also needs other columns (a template naming
any column of the table, say) reads the table itself with
w.fetchRows(tableId), which needs full access and follows the
document's access rules.
Write under your names
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "full",
columns: [
{ name: "Title", type: "Text" },
{ name: "Done", type: "Bool" },
],
})
export default function App() {
const w = widget.useGrist()
if (!w.mappedRecord) return <p>Select a task.</p>
const { Done } = w.mappedRecord
return <button onClick={() => w.updateRecord({ Done: !Done })}>{Done ? "Reopen" : "Done"}</button>
}w.updateRecord writes the selected row, and w.addRecord a new one,
under your names: the SDK writes each into the column mapped to it, and
encodes the value for its declared type. A name mapped to several columns
(allowMultiple) isn't written, since nothing says which of them should
change; the "Write skipped" alert names it. See
Write data.
Several columns under one name
With allowMultiple, the person maps any number of columns to one name,
and w.mappedRecord.Tags holds one value per mapped column, in their
order: a widget showing "every tag column" reads them as one list.