Documentation
¶
Overview ¶
Package table renders an editable HTML table (grid) widget whose columns are defined by a form.Element and whose data is described by a rosetta schema.
Index ¶
- type IconProvider
- type Table
- func (widget Table) AllowAdd() Table
- func (widget Table) AllowAll() Table
- func (widget Table) AllowDelete() Table
- func (widget Table) AllowEdit() Table
- func (widget Table) AllowNone() Table
- func (widget Table) Do(queryParams *url.URL, data map[string]any) error
- func (widget Table) DoDelete(deleteIndex int) error
- func (widget Table) DoEdit(data map[string]any, editIndex int) error
- func (widget Table) Draw(params *url.URL, buffer io.Writer) error
- func (widget Table) DrawAdd(buffer io.Writer) error
- func (widget Table) DrawAddString() (string, error)
- func (widget Table) DrawEdit(index int, buffer io.Writer) error
- func (widget Table) DrawEditString(index int) (string, error)
- func (widget Table) DrawView(buffer io.Writer) error
- func (widget Table) DrawViewString() (string, error)
- func (widget Table) UseLookupProvider(lookupProvider form.LookupProvider) Table
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IconProvider ¶
type IconProvider interface {
// Get returns the HTML for the named icon.
Get(name string) string
// Write writes the HTML for the named icon to the provided writer.
Write(name string, writer io.Writer)
}
IconProvider generates the HTML for the icons used by the table's controls.
type Table ¶
type Table struct {
// Required Fields
Schema *schema.Schema // Data schema for the data in this table
Form *form.Element // Form (UI Schema) that defines the columns being displayed
Object any // Object containing the table data
Path string // Path to the data in the object
TargetURL string // URL to send the form data to
Icons IconProvider // IconProvider generates HTML for icons
// Optional Fields
LookupProvider form.LookupProvider // Optional dependency to provide lookup data for fields
CanAdd bool // If TRUE, then users can add new rows to the table
CanEdit bool // If TRUE, then users can edit existing rows in the table
CanDelete bool // If TRUE, then users can delete existing rows in the table
}
Table defines all of the properties of a table widget
Example ¶
ExampleTable builds a read-only table and prints part of the generated markup.
Two details are easy to get wrong, and the Output below is what catches them: the row data has to be in a type rosetta can walk into (a plain []map[string]any is not), and every column's Type has to name a form widget that is actually registered.
// Data schema defines the layout of the data.
s := schema.New(schema.Array{
MaxLength: 10,
Items: schema.Object{
Properties: schema.ElementMap{
"name": schema.String{},
"age": schema.Integer{},
},
},
})
// UI schema defines which fields are displayed, and in which order. The "text"
// widget takes its input type from the data schema, so the "age" column still
// renders as <input type="number">.
f := form.Element{
Type: "layout-vertical",
Children: []form.Element{
{Type: "text", Label: "Name", Path: "name"},
{Type: "text", Label: "Age", Path: "age"},
},
}
// Define some data to render.
data := sliceof.Object[mapof.Any]{
{"name": "John Connor", "age": 20},
{"name": "Sarah Connor", "age": 45},
}
// Create the new table and render it as HTML. The last argument before the URL
// is your own IconProvider, which supplies the markup for the row controls.
table := New(&s, &f, &data, "", testIconProvider{}, "/update-form")
result, err := table.DrawViewString()
if err != nil {
fmt.Println("Error:", err)
return
}
// Print a digestible slice of the markup rather than all ~1.6kb of it.
fmt.Println(strings.Count(result, `<tr class="grid-row`), "data rows")
fmt.Println(result[strings.Index(result, "<tr") : strings.Index(result, "</tr>")+len("</tr>")])
Output: 2 data rows <tr class="grid-header"><td class="grid-cell"><div>Name</div></td><td class="grid-cell"><div>Age</div></td><td class="grid-cell grid-controls"></td></tr>
func New ¶
func New(schema *schema.Schema, form *form.Element, object any, path string, iconProvider IconProvider, targetURL string) Table
New returns a fully initialized Table widget (with all required fields)
func (Table) AllowAll ¶
AllowAll returns a copy of the table that allows all write actions (Add, Edit, Delete).
func (Table) AllowDelete ¶
AllowDelete returns a copy of the table that allows deleting existing rows.
func (Table) Do ¶ added in v0.2.0
Do applies an edit or delete action to the table's data, selecting the action from the "edit" and "delete" query parameters.
func (Table) Draw ¶ added in v0.2.0
Draw renders the table to the buffer, choosing view, add, or edit mode based on the "add", "edit", and "focus" query parameters.
func (Table) DrawAddString ¶
DrawAddString returns a string representation of the table with a row for adding a new record
func (Table) DrawEditString ¶
DrawEditString returns a string representation of the table with a single editable row
func (Table) DrawViewString ¶
DrawViewString returns a string representation of the table (VIEW ONLY)
func (Table) UseLookupProvider ¶
func (widget Table) UseLookupProvider(lookupProvider form.LookupProvider) Table
UseLookupProvider returns a copy of the table that uses the given lookup provider.