table

package module
v0.6.16 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: Apache-2.0 Imports: 11 Imported by: 2

README

Table 🍽

GoDoc Version Build Status Go Report Card Codecov

Inline editor grid component for Go and htmx.

This repository implements a table/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 defined 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: null.NewInt(10),
		Items: schema.Object{
			Properties: schema.ElementMap{
				"name": schema.String{},
				"age":  schema.Integer{},
			},
		},
	})

	// UI schema defines which field are displayed, and in which order
	f := form.Element{
		Type: "layout-vertical",
		Children: []form.Element{
			{Type: "text", Label: "Name", Path: "name"},
			{Type: "number", Label: "Age", Path: "age"},
		},
	}

	// Define some data to render
	data := []maps.Map{
		{"name": "John Connor", "age": 20},
		{"name": "Sarah Connor", "age": 45},
	}

	// Create the new table and render it in HTML
	table := New(&s, &f, &data, "", bootstrap.Provider{}, "http://localhost/update-form")
	fmt.Println(table.DrawViewString())
}

DO NOT USE

This project is a work-in-progress, and should NOT be used by ANYONE, for ANY PURPOSE, under ANY CIRCUMSTANCES. It is 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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IconProvider

type IconProvider interface {
	Get(name string) string
	Write(name string, writer io.Writer)
}

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
// 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 field are displayed, and in which order
f := form.Element{
	Type: "layout-vertical",
	Children: []form.Element{
		{Type: "text", Label: "Name", Path: "name"},
		{Type: "number", Label: "Age", Path: "age"},
	},
}

// Define some data to render
data := []map[string]any{
	{"name": "John Connor", "age": 20},
	{"name": "Sarah Connor", "age": 45},
}

// Create the new table and render it in HTML
table := New(&s, &f, &data, "", testIconProvider{}, "http://localhost/update-form")
fmt.Println(table.DrawViewString())
Output:

func New

func New(schema *schema.Schema, form *form.Element, object any, path string, iconProvider IconProvider, targetURL string) Table

New returns a fully initialiized Table widget (with all required fields)

func (*Table) AllowAdd

func (widget *Table) AllowAdd() *Table

AllowAdd modifies the table to allow adding new rows.

func (*Table) AllowAll

func (widget *Table) AllowAll() *Table

AllowAll modifies the table to allow all write actions (Add, Edit, Delete).

func (*Table) AllowDelete

func (widget *Table) AllowDelete() *Table

AllowDelete modifies the table to allow deleting existing rows.

func (*Table) AllowEdit

func (widget *Table) AllowEdit() *Table

AllowEdit modifies the table to allow editing existing rows.

func (*Table) AllowNone

func (widget *Table) AllowNone() *Table

AllowNone modifies the table to disallow all write actions.

func (*Table) Do added in v0.2.0

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

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

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 modifies the table to use the given lookup provider.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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