table

package module
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Table 🍽

Go Reference Version Build Status Go Report Card Codecov

Inline Editor Grid Component for Go and htmx

This repository implements a table or grid control with inline editing. Tables are rendered on the server and swapped into the DOM via htmx hx-get and hx-post methods.

Tables work similar to other form widgets, and require that you define both a data schema and a UI schema in order to render them.

func ExampleTable() {

	// 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.  Each
	// Type must name a registered form widget (see form/widget.UseAll).  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.  The row data must live in a type that rosetta
	// can index into -- sliceof.Object/mapof.Any here.  A plain []map[string]any
	// will render the header and then fail on the first row.
	data := sliceof.Object[mapof.Any]{
		{"name": "John Connor", "age": 20},
		{"name": "Sarah Connor", "age": 45},
	}

	// Create the new table and render it in HTML.  iconProvider is your own
	// implementation of the IconProvider interface, which supplies the markup for
	// the add, edit, and delete controls.
	table := New(&s, &f, &data, "", iconProvider, "http://localhost/update-form")

	result, err := table.DrawViewString()

	if err != nil {
		panic(err)
	}

	fmt.Println(result)
}

DO NOT USE

This project is a work-in-progress, and should NOT be used by ANYONE, for ANY PURPOSE, under ANY CIRCUMSTANCES. It WILL BE CHANGED UNDERNEATH YOU WITHOUT NOTICE OR HESITATION, and is expressly GUARANTEED to blow up your computer, send your cat into an infinite loop, and combine your hot and cold laundry into a single cycle.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 🍽

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

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) AllowAdd

func (widget Table) AllowAdd() Table

AllowAdd returns a copy of the table that allows adding new rows.

func (Table) AllowAll

func (widget Table) AllowAll() Table

AllowAll returns a copy of the table that allows all write actions (Add, Edit, Delete).

func (Table) AllowDelete

func (widget Table) AllowDelete() Table

AllowDelete returns a copy of the table that allows deleting existing rows.

func (Table) AllowEdit

func (widget Table) AllowEdit() Table

AllowEdit returns a copy of the table that allows editing existing rows.

func (Table) AllowNone

func (widget Table) AllowNone() Table

AllowNone returns a copy of the table that disallows all write actions.

func (Table) Do added in v0.2.0

func (widget Table) Do(queryParams *url.URL, data map[string]any) error

Do applies an edit or delete action to the table's data, selecting the action from the "edit" and "delete" query parameters.

func (Table) DoDelete

func (widget Table) DoDelete(deleteIndex int) error

DoDelete removes the requested row from the table

func (Table) DoEdit

func (widget Table) DoEdit(data map[string]any, editIndex int) error

DoEdit applies a dataset to the requested row in the table

func (Table) Draw added in v0.2.0

func (widget Table) Draw(params *url.URL, buffer io.Writer) error

Draw renders the table to the buffer, choosing view, add, or edit mode based on the "add", "edit", and "focus" query parameters.

func (Table) DrawAdd

func (widget Table) DrawAdd(buffer io.Writer) error

DrawAdd returns the table with a row for adding a new record

func (Table) DrawAddString

func (widget Table) DrawAddString() (string, error)

DrawAddString returns a string representation of the table with a row for adding a new record

func (Table) DrawEdit

func (widget Table) DrawEdit(index int, buffer io.Writer) error

DrawEdit returns the table with a single editable row

func (Table) DrawEditString

func (widget Table) DrawEditString(index int) (string, error)

DrawEditString returns a string representation of the table with a single editable row

func (Table) DrawView

func (widget Table) DrawView(buffer io.Writer) error

DrawView returns a VIEW ONLY representation of the table

func (Table) DrawViewString

func (widget Table) DrawViewString() (string, error)

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.

Directories

Path Synopsis
Command server is a small demo HTTP server that renders the table widget and updates an in-memory database as the user edits it.
Command server is a small demo HTTP server that renders the table widget and updates an in-memory database as the user edits it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址