table

package
v0.31.10 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package table implements various table controls.

These controls output HTML table tags, and have [column] objects for displaying different kinds of data in each column. Table is the main implementation of a table, while SelectTable and PagedTable our specialized.

Index

Constants

View Source
const (
	NotSortable    = SortDirection(0)
	SortAscending  = SortDirection(1)
	SortDescending = SortDirection(-1)
	NotSorted      = SortDirection(-100)
)
View Source
const (
	ColumnAction = iota + 2000
	SortClick
)
View Source
const ColumnSortEventName = "gr-sort"

Variables

View Source
var SortButtonHtmlGetter func(SortDirection) string

SortButtonHtmlGetter is the injected function for getting the html for sort buttons in the column header.

Functions

func ColumnSortEvent

func ColumnSortEvent() *event.Event

ColumnSortEvent indicates a column's sort button has been clicked.

func RegisterColumn

func RegisterColumn(i interface{})

RegisterColumn registers the column for the serialize/deserialize process. You should call this for each column type in an init() function.

func RowSelectedEvent

func RowSelectedEvent() *event.Event

RowSelectedEvent is an event that indicates a row was selected on a SelectTable.

Types

type CellInfo

type CellInfo struct {
	RowNum int
	ColNum int
	Data   interface{}
	// contains filtered or unexported fields
}

CellInfo is provided to the cell texter so the cell texter knows how to draw. It is a struct here so that the info can grow without the CellTexter signature having to change.

type CellStyler

type CellStyler interface {
	CellAttributes(ctx context.Context, col ColumnI, info CellInfo) html5tag.Attributes
}

CellStyler defines the interface that returns attributes specific to a particular cell.

type CellTexter

type CellTexter interface {
	CellText(ctx context.Context, col ColumnI, info CellInfo) string
}

CellTexter defines the interface for a structure that provides the content of a table cell. If your CellTexter is not a control, you should register it with gob.

type ColumnBase

type ColumnBase struct {
	base.Base

	html5tag.Attributes // These are static attributes that will appear on each cell
	// contains filtered or unexported fields
}

ColumnBase is the base implementation of all table columns

func (*ColumnBase) AddActions

func (c *ColumnBase) AddActions(ctrl page.ControlI)

func (*ColumnBase) ApplyFormat

func (c *ColumnBase) ApplyFormat(data interface{}) string

ApplyFormat is used by table columns to apply the given fmt.Sprintf and time.Format strings to the data. It is exported to allow custom cell Texter objects to use it. For slice data, the format is applied to each item in the slice and each item is separated with a comma.

func (*ColumnBase) ApplyOptions

func (c *ColumnBase) ApplyOptions(ctx context.Context, parent TableI, opt ColumnOptions)

func (*ColumnBase) AsHeader

func (c *ColumnBase) AsHeader() bool

AsHeader returns true if the cells in the column will be drawn as header cells (th).

func (*ColumnBase) CellAttributes

func (c *ColumnBase) CellAttributes(ctx context.Context, row int, col int, data interface{}) html5tag.Attributes

CellAttributes returns the attributes of the cell. Column implementations should call this base version first before customizing more. It will use the CellStyler if one was provided.

func (*ColumnBase) CellData

func (c *ColumnBase) CellData(ctx context.Context, row int, col int, data interface{}) interface{}

CellData returns the data in the cell.

func (*ColumnBase) CellText

func (c *ColumnBase) CellText(ctx context.Context, row int, col int, data interface{}) string

CellText returns the text in the cell. It will use the CellTexter if one was provided.

func (*ColumnBase) CellTexter

func (c *ColumnBase) CellTexter() CellTexter

CellTexter returns the cell texter.

func (*ColumnBase) ColTagAttributes

func (c *ColumnBase) ColTagAttributes() html5tag.Attributes

ColTagAttributes specifies attributes that will appear in the column tag. Note that you have to turn on column tags in the table object as well for these to appear.

func (*ColumnBase) Deserialize

func (c *ColumnBase) Deserialize(dec page.Decoder)

func (*ColumnBase) DoAction

func (c *ColumnBase) DoAction(ctx context.Context, params action.Params)

DoAction does an action is directed at this column. Column implementations can implement this method to receive private actions that they have added using AddActions. By default, the action will be passed to the parent table if not handled.

func (*ColumnBase) DrawCell

func (c *ColumnBase) DrawCell(ctx context.Context, row int, col int, data interface{}, w io.Writer)

DrawCell is the default cell drawing function.

func (*ColumnBase) DrawColumnTag

func (c *ColumnBase) DrawColumnTag(ctx context.Context, w io.Writer)

DrawColumnTag draws the column tag in the colgroup.

func (*ColumnBase) DrawFooterCell

func (c *ColumnBase) DrawFooterCell(ctx context.Context, row int, col int, count int, w io.Writer)

DrawFooterCell will draw the footer cells html into the given buffer.

func (*ColumnBase) FooterAttributes

func (c *ColumnBase) FooterAttributes(ctx context.Context, row int, col int) html5tag.Attributes

FooterAttributes returns the attributes to use for the footer cell.

func (*ColumnBase) FooterCellHtml

func (c *ColumnBase) FooterCellHtml(ctx context.Context, row int, col int) string

FooterCellHtml returns the html to use in the given footer cell.

func (*ColumnBase) HeaderAttributes

func (c *ColumnBase) HeaderAttributes(ctx context.Context, row int, col int) html5tag.Attributes

HeaderAttributes returns the attributes to use on the header cell. The default version will return an attribute structure which you can use to directly manipulate the attributes. If you want something more customized, create your own column and implement this function. row and col are zero based.

func (*ColumnBase) HeaderCellHtml

func (c *ColumnBase) HeaderCellHtml(ctx context.Context, row int, col int) (h string)

HeaderCellHtml returns the text of the indicated header cell. The default will call into the headerTexter if it is provided, or just return the Label value. This function can also be overridden by embedding the ColumnBase object into another object.

func (*ColumnBase) ID

func (c *ColumnBase) ID() string

ID returns the id of the column

func (*ColumnBase) Init

func (c *ColumnBase) Init(self ColumnI)

func (*ColumnBase) IsHidden

func (c *ColumnBase) IsHidden() bool

IsHidden returns true if the column is hidden.

func (*ColumnBase) IsSortable

func (c *ColumnBase) IsSortable() bool

IsSortable indicates whether the column is sortable, and has a sort indicator in the head.

func (*ColumnBase) MarshalState

func (c *ColumnBase) MarshalState(m page.SavedState)

MarshalState is an internal function to save the state of the control.

func (*ColumnBase) ParentTable

func (c *ColumnBase) ParentTable() TableI

ParentTable returns the table that is the parent of the column

func (*ColumnBase) PreRender

func (c *ColumnBase) PreRender()

PreRender is called just before the table is redrawn.

func (*ColumnBase) RenderSortButton

func (c *ColumnBase) RenderSortButton(labelHtml string) string

RenderSortButton returns the HTML that draws the sort button.

func (*ColumnBase) Restore

func (c *ColumnBase) Restore(parentTable TableI)

func (*ColumnBase) Serialize

func (c *ColumnBase) Serialize(e page.Encoder)

func (*ColumnBase) SetAsHeader

func (c *ColumnBase) SetAsHeader(r bool)

SetAsHeader will cause the entire column to be output with th instead of td cells.

func (*ColumnBase) SetCellStyler

func (c *ColumnBase) SetCellStyler(s CellStyler)

SetCellStyler sets the CellStyler for the body cells.

func (*ColumnBase) SetCellTexter

func (c *ColumnBase) SetCellTexter(s CellTexter) ColumnI

SetCellTexter sets the CellTexter for getting the content of each body cell.

func (*ColumnBase) SetFooterTexter

func (c *ColumnBase) SetFooterTexter(s CellTexter) ColumnI

SetFooterTexter sets the CellTexter that gets the text for footer cells.

func (*ColumnBase) SetFormat

func (c *ColumnBase) SetFormat(format string) ColumnI

SetFormat sets string that will be passed to fmt.Sprintf with the data of the cell to determine how to draw the cell.

func (*ColumnBase) SetHeaderTexter

func (c *ColumnBase) SetHeaderTexter(s CellTexter) ColumnI

SetHeaderTexter sets the CellTexter that gets the text for header cells.

func (*ColumnBase) SetHidden

func (c *ColumnBase) SetHidden(h bool) ColumnI

SetHidden hides the column without removing it completely from the table.

func (*ColumnBase) SetID

func (c *ColumnBase) SetID(id string) ColumnI

SetID sets the id of the column. If you are going to provide your own id, do this as the first thing after you create a table, or the new id might not propagate through the system correctly. Note that the id in html will have the table id prepended to it. This is required so that actions can be routed to a column.

func (*ColumnBase) SetIsHtml

func (c *ColumnBase) SetIsHtml(columnIsHtml bool) ColumnI

SetIsHtml will cause the cell to treat the text it receives as html rather than raw text it should escape. Use this with extreme caution. Do not display unescaped text that might come from user input, as it could open you up to XSS attacks.

func (*ColumnBase) SetShowLocalTime added in v0.26.0

func (c *ColumnBase) SetShowLocalTime(showLocalTime bool) ColumnI

SetShowLocalTime will convert a time into client local time for display.

func (*ColumnBase) SetSortDirection

func (c *ColumnBase) SetSortDirection(d SortDirection) ColumnI

SetSortDirection is used internally to set the sort direction indicator.

func (*ColumnBase) SetSortable

func (c *ColumnBase) SetSortable() ColumnI

SetSortable indicates that the column should be drawn with sort indicators.

func (*ColumnBase) SetSpan

func (c *ColumnBase) SetSpan(span int) ColumnI

SetSpan sets the span indicated in the column tag of the column. This is used to create colgroup tags.

func (*ColumnBase) SetTimeFormat

func (c *ColumnBase) SetTimeFormat(timeFormat string) ColumnI

SetTimeFormat is the format string sent to time.Format and that is used specifically to format time values in the cell. If both a time format and cell format are defined, the time format will be used first to convert the time.Time into a string, and then the result will be sent to fmt.Sprintf using the format string.

func (*ColumnBase) SetTitle

func (c *ColumnBase) SetTitle(title string) ColumnI

SetTitle sets the title of the column. It returns a column reference for chaining.

func (*ColumnBase) SortDirection

func (c *ColumnBase) SortDirection() SortDirection

SortDirection returns the current sort direction.

func (*ColumnBase) Span

func (c *ColumnBase) Span() int

Span returns the number of columns this column will span. If the span is not set, it will return 1.

func (*ColumnBase) Title

func (c *ColumnBase) Title() string

Title returns the title text that will appear in the header of the column

func (*ColumnBase) UnmarshalState

func (c *ColumnBase) UnmarshalState(m page.SavedState)

UnmarshalState is an internal function to restore the state of the control.

func (*ColumnBase) UpdateFormValues

func (c *ColumnBase) UpdateFormValues(ctx context.Context)

UpdateFormValues is called by the system whenever values are sent by client controls. This default version does nothing. Columns that need to record information (checkbox columns for example), should implement this.

type ColumnCreator

type ColumnCreator interface {
	Create(context.Context, TableI) ColumnI
}

func Columns

func Columns(cols ...ColumnCreator) []ColumnCreator

Columns is a helper to return a group of columns

type ColumnI

type ColumnI interface {
	ID() string

	SetID(string) ColumnI

	ParentTable() TableI
	Title() string
	SetTitle(string) ColumnI
	Span() int
	SetSpan(int) ColumnI
	IsHidden() bool
	AsHeader() bool
	SetHidden(bool) ColumnI
	DrawColumnTag(ctx context.Context, w io.Writer)
	DrawFooterCell(ctx context.Context, row int, col int, count int, w io.Writer)
	DrawCell(ctx context.Context, row int, col int, data interface{}, w io.Writer)
	CellText(ctx context.Context, row int, col int, data interface{}) string
	CellData(ctx context.Context, row int, col int, data interface{}) interface{}
	HeaderCellHtml(ctx context.Context, row int, col int) string
	FooterCellHtml(ctx context.Context, row int, col int) string
	HeaderAttributes(ctx context.Context, row int, col int) html5tag.Attributes
	FooterAttributes(ctx context.Context, row int, col int) html5tag.Attributes
	ColTagAttributes() html5tag.Attributes
	UpdateFormValues(ctx context.Context)
	AddActions(ctrl page.ControlI)
	DoAction(ctx context.Context, params action.Params)
	SetCellTexter(s CellTexter) ColumnI
	SetHeaderTexter(s CellTexter) ColumnI
	SetFooterTexter(s CellTexter) ColumnI
	SetCellStyler(s CellStyler)
	IsSortable() bool
	SortDirection() SortDirection
	SetSortDirection(SortDirection) ColumnI
	SetSortable() ColumnI
	RenderSortButton(labelHtml string) string
	SetIsHtml(columnIsHtml bool) ColumnI
	PreRender()
	MarshalState(m page.SavedState)
	UnmarshalState(m page.SavedState)
	Serialize(e page.Encoder)
	Deserialize(dec page.Decoder)
	Restore(parentTable TableI)
	// contains filtered or unexported methods
}

ColumnI defines the interface that all columns must support. Most of these functions are provided by the default behavior of the ColumnBase class.

type ColumnOptions

type ColumnOptions struct {
	// CellAttributes is a static map of attributes to apply to every cell in the column
	CellAttributes html5tag.Attributes
	// HeaderAttributes is a slice of attributes to apply to each row of the header cells in the column.
	// Each item in the slice corresponds to a row of the header.
	HeaderAttributes []html5tag.Attributes
	// FooterAttributes is a slice of attributes to apply to each row of the footer cells in the column.
	// Each item in the slice corresponds to a row of the footer.
	FooterAttributes []html5tag.Attributes
	// ColTagAttributes applies attributes to the col tag if col tags are on in the table. There are limited uses for
	// this, but in particular, you can style a column and give it an id. Use Span to set the span attribute.
	ColTagAttributes html5tag.Attributes
	// Span is specifically for col tags to specify the width of the styling in the col tag.
	Span int
	// AsHeader will cause the entire column to output header tags (th) instead of standard cell tags (td).
	// This is useful for columns on the left or right that contain labels for the rows.
	AsHeader bool
	// IsHtml will cause the text of the cells to NOT be escaped
	IsHtml bool
	// HeaderTexter is an object that will provide the text of the header cells.
	// This can be either an object that you have set up prior, or a string id of a control.
	HeaderTexter interface{}
	// FooterTexter is an object that will provide the text of the footer cells.
	// This can be either an object that you have set up prior, or a string id of a control.
	FooterTexter interface{}
	// IsHidden will start the column out in a hidden state so that it will not initially be drawn.
	IsHidden bool
	// Format is a format string applied to the data using fmt.Sprintf.
	Format string
	// TimeFormat is a format string applied specifically to time data using time.Format.
	TimeFormat string
	// ShowLocalTime will convert the time to the client's local time.
	ShowLocalTime bool
}

ColumnOptions are settings you can apply to all types of table columns

type FooterRowAttributer

type FooterRowAttributer interface {
	// FooterRowAttributes returns attributes to use for the particular footer row indicated
	FooterRowAttributes(row int) html5tag.Attributes
}

type HeaderRowAttributer

type HeaderRowAttributer interface {
	// HeaderRowAttributes returns attributes to use for the particular header row indicated
	HeaderRowAttributes(row int) html5tag.Attributes
}

type PagedTable

type PagedTable struct {
	Table
	control2.PagedControl
}

func GetPagedTable

func GetPagedTable(c page.ControlI, id string) *PagedTable

GetPagedTable is a convenience method to return the table with the given id from the page.

func NewPagedTable

func NewPagedTable(parent page.ControlI, id string) *PagedTable

func (*PagedTable) Deserialize

func (t *PagedTable) Deserialize(dec page.Decoder)

func (*PagedTable) Init

func (t *PagedTable) Init(self any, parent page.ControlI, id string)

func (*PagedTable) MarshalState

func (t *PagedTable) MarshalState(m page.SavedState)

MarshalState is an internal function to save the state of the control

func (*PagedTable) Serialize

func (t *PagedTable) Serialize(e page.Encoder)

func (*PagedTable) UnmarshalState

func (t *PagedTable) UnmarshalState(m page.SavedState)

UnmarshalState is an internal function to restore the state of the control

type PagedTableCreator

type PagedTableCreator struct {
	// ID is the control id
	ID string
	// Caption is the content of the caption tag, and can either be a string, or a data pager
	Caption interface{}
	// HideIfEmpty will hide the table completely if it has no data. Otherwise, the table and headers will be shown, but no data rows
	HideIfEmpty bool
	// HeaderRowCount is the number of header rows. You must set this to at least 1 to show header rows.
	HeaderRowCount int
	// FooterRowCount is the number of footer rows.
	FooterRowCount int
	// RowStyler returns the attributes to be used in a cell.
	RowStyler RowAttributer
	// RowStylerID is a control id for the control that will be the RowStyler of the table.
	RowStylerID string
	// HeaderRowStyler returns the attributes to be used in a header cell.
	HeaderRowStyler HeaderRowAttributer
	// HeaderRowStylerID is a control id for the control that will be the HeaderRowStyler of the table.
	HeaderRowStylerID string
	// FooterRowStyler returns the attributes to be used in a footer cell. It can be either a control id or a FooterRowAttributer.
	FooterRowStyler FooterRowAttributer
	// FooterRowStylerID is a control id for the control that will be the FooterRowStyler of the table.
	FooterRowStylerID string
	// Columns are the column creators that will add columns to the table
	Columns []ColumnCreator
	// DataProvider is the data binder for the table. It can be either a control id or a DataBinder
	DataProvider control2.DataBinder
	// DataProviderID is the control id of the data binder for the table.
	DataProviderID string
	// Data is the actual data for the table, and should be a slice of objects
	Data interface{}
	// Sortable will make the table sortable
	Sortable bool
	// SortHistoryLimit will set how many columns deep we will remember the sorting for multi-level sorts
	SortHistoryLimit int
	// SortColumnIDs is the list of column ids that will be the initial sort order.
	SortColumnIDs []string
	OnCellClick   action.CallbackActionI
	page.ControlOptions
	// PageSize is the number of rows to include in a page
	PageSize int
	// SaveState will cause the table to remember what page it was on
	SaveState bool
}

PagedTableCreator creates a table that can be paged

func (PagedTableCreator) Create

func (c PagedTableCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI

Create is called by the framework to create a new control from the Creator. You do not normally need to call this.

func (PagedTableCreator) Init

func (c PagedTableCreator) Init(ctx context.Context, ctrl PagedTableI)

Init is called by implementations of Buttons to initialize a control with the creator.

type PagedTableI

type PagedTableI interface {
	TableI
	control2.PagedControlI
}

type PrimaryKeyer

type PrimaryKeyer interface {
	PrimaryKey() string
}

PrimaryKeyer is an interface that is often implemented by model objects.

type RowAttributer

type RowAttributer interface {
	// RowAttributes returns attributes that should be used on the particular row indicated.
	// Data is the data for that row.
	RowAttributes(row int, data interface{}) html5tag.Attributes
}

RowAttributer is used to style particular table rows.

type SelectTable

type SelectTable struct {
	Table
	// contains filtered or unexported fields
}

SelectTable is a table that is row selectable. To detect a row selection, using the RowSelectedEvent event.

func GetSelectTable

func GetSelectTable(c page.ControlI, id string) *SelectTable

GetSelectTable is a convenience method to return the button with the given id from the page.

func NewSelectTable

func NewSelectTable(parent page.ControlI, id string) *SelectTable

NewSelectTable creates a new SelectTable, which is a table with clickable rows.

func (*SelectTable) Deserialize

func (t *SelectTable) Deserialize(dec page.Decoder)

func (*SelectTable) DrawingAttributes

func (t *SelectTable) DrawingAttributes(ctx context.Context) html5tag.Attributes

DrawingAttributes is called by the framework to style the table tag.

func (*SelectTable) Init

func (t *SelectTable) Init(self any, parent page.ControlI, id string)

func (*SelectTable) MarshalState

func (t *SelectTable) MarshalState(m page.SavedState)

func (*SelectTable) RowAttributes

func (t *SelectTable) RowAttributes(row int, data interface{}) (a html5tag.Attributes)

RowAttributes is an override to return the rows identified by an id.

func (*SelectTable) SelectedID

func (t *SelectTable) SelectedID() string

SelectedID returns the id of the currently selected row.

func (*SelectTable) Serialize

func (t *SelectTable) Serialize(e page.Encoder)

func (*SelectTable) SetReselectable

func (t *SelectTable) SetReselectable(r bool) SelectTableI

SetReselectable sets whether the user can send a select command when tapping the currently selected item.

func (*SelectTable) SetSelectedID

func (t *SelectTable) SetSelectedID(id string) SelectTableI

SetSelectedID sets the id of the selected row.

func (*SelectTable) UnmarshalState

func (t *SelectTable) UnmarshalState(m page.SavedState)

func (*SelectTable) UpdateFormValues

func (t *SelectTable) UpdateFormValues(ctx context.Context)

UpdateFormValues is called by the framework to get its selected row from incoming form values.

func (*SelectTable) Value

func (t *SelectTable) Value() interface{}

Value satisfies the Valuer interface and returns the selected row id.

type SelectTableCreator

type SelectTableCreator struct {

	// ID is the control id
	ID string
	// Caption is the content of the caption tag, and can either be a string, or a data pager
	Caption interface{}
	// HideIfEmpty will hide the table completely if it has no data. Otherwise, the table and headers will be shown, but no data rows
	HideIfEmpty bool
	// HeaderRowCount is the number of header rows. You must set this to at least 1 to show header rows.
	HeaderRowCount int
	// FooterRowCount is the number of footer rows.
	FooterRowCount int
	// RowStyler returns the attributes to be used in a cell.
	RowStyler RowAttributer
	// RowStylerID is a control id for the control that will be the RowStyler of the table.
	RowStylerID string
	// HeaderRowStyler returns the attributes to be used in a header cell.
	HeaderRowStyler HeaderRowAttributer
	// HeaderRowStylerID is a control id for the control that will be the HeaderRowStyler of the table.
	HeaderRowStylerID string
	// FooterRowStyler returns the attributes to be used in a footer cell. It can be either a control id or a FooterRowAttributer.
	FooterRowStyler FooterRowAttributer
	// FooterRowStylerID is a control id for the control that will be the FooterRowStyler of the table.
	FooterRowStylerID string
	// Columns are the column creators that will add columns to the table
	Columns []ColumnCreator
	// DataProvider is the control that will dynamically provide the data for the list and that implements the DataBinder interface.
	DataProvider control.DataBinder
	// DataProviderID is the id of a control that will dynamically provide the data for the list and that implements the DataBinder interface.
	DataProviderID string
	// Data is the actual data for the table, and should be a slice of objects
	Data interface{}
	// Sortable will make the table sortable
	Sortable bool
	// SortHistoryLimit will set how many columns deep we will remember the sorting for multi-level sorts
	SortHistoryLimit int
	page.ControlOptions
	// OnRowSelected is the action to take when the row is selected
	OnRowSelected action.ActionI
	// SelectedID is the row id that will start as the selection
	SelectedID string
	// Reselectable determines if you will get a select command when the user taps the item that is already selected.
	Reselectable bool
	// SaveState will cause the table to remember the selection
	SaveState bool
}

SelectTableCreator is the initialization structure for declarative creation of tables

func (SelectTableCreator) Create

Create is called by the framework to create a new control from the Creator. You do not normally need to call this.

func (SelectTableCreator) Init

func (c SelectTableCreator) Init(ctx context.Context, ctrl SelectTableI)

Init is called by implementations of Buttons to initialize a control with the creator.

type SelectTableI

type SelectTableI interface {
	TableI
	SetSelectedID(id string) SelectTableI
	SetReselectable(r bool) SelectTableI
}

type SortDirection

type SortDirection int

SortDirection indicates the current direction of the sort arrows for the column.

  • NotSortable indicates the column cannot be sorted.
  • NotSorted indicates that the column CAN be sorted, but the direction is currently not determined
  • SortAscending and SortDescending indicate the column is currently sorted in the corresponding direction.

type Table

type Table struct {
	page.ControlBase
	control2.DataManager
	// contains filtered or unexported fields
}

Table is a GoRADD control that outputs a dynamic HTML table object, with table, tr, th and td tags, as well as optional col, thead, and tfoot tags.

To use a Table, call NewTable and then add column objects to it. The columns use a CellTexter to draw the contents of a cell in the table. There are a number of predefined columns to draw text coming from slices of maps, slices, database objects, as well as custom functions you define. See the examples directory for examples of using a Table object. See also the PagedTable for a table that works with a Pager object to page through a large data set.

Call MakeSortable() to make a table sortable, in which case the user can click in the header of a column to sort by that column. The Table maintains a history of what columns have been sorted by what row, so that you can implement multi-level sorting if you so desire. This is particularly helpful when some columns have duplicate data, that then get further identified by another column.

You can provide data to the table when it is first created by calling SetData, or you can provide the data just before drawing by calling SetDataProvider at initialization of the table, and then providing the table data in response to the BindData function. The advantage of this approach is that if you are showing a lot of data, the data is only loaded in memory during drawing, and not kept in the pagestate after drawing. It also limits the amount of data being loaded to just what items will be visible.

Another way to customize a table is to create a subclass of a standard table and implement your own functions for drawing cells, rows, headers, etc.

func GetTable

func GetTable(c page.ControlI, id string) *Table

GetTable is a convenience method to return the table with the given id from the page.

func NewTable

func NewTable(parent page.ControlI, id string) *Table

NewTable creates a new table

func (*Table) AddColumn

func (t *Table) AddColumn(column ColumnI) ColumnI

AddColumn adds the given column to the end of the column list.

func (*Table) AddColumnAt

func (t *Table) AddColumnAt(column ColumnI, loc int)

AddColumnAt adds the given column at the column offset given. 0 is the first column location. Pass a negative number or a number bigger than the current number of columns to put it at the end.

func (*Table) ClearColumns

func (t *Table) ClearColumns()

ClearColumns removes all the columns.

func (*Table) Deserialize

func (t *Table) Deserialize(dec page.Decoder)

func (*Table) DoPrivateAction

func (t *Table) DoPrivateAction(ctx context.Context, p action.Params)

DoPrivateAction is called by the framework to allow controls to process actions internal to themselves. This implementation directs actions to columns and handles sorting clicks.

func (*Table) DrawCaption

func (t *Table) DrawCaption(ctx context.Context, w io.Writer)

DrawCaption is called internally to draw the caption. Subclasses can override this to draw a custom caption.

func (*Table) DrawColumnTags

func (t *Table) DrawColumnTags(ctx context.Context, w io.Writer)

DrawColumnTags is called by the framework to draw the column tags.

func (*Table) DrawFooterRows

func (t *Table) DrawFooterRows(ctx context.Context, w io.Writer)

DrawFooterRows is called by the framework to draw the tf rows of the table.

func (*Table) DrawHeaderRows

func (t *Table) DrawHeaderRows(ctx context.Context, w io.Writer)

DrawHeaderRows is called by the framework to call the header rows of the table.

func (*Table) DrawInnerHtml

func (t *Table) DrawInnerHtml(ctx context.Context, w io.Writer)

DrawInnerHtml is an override to draw the meat of the table.

func (*Table) DrawRow

func (t *Table) DrawRow(ctx context.Context, row int, data interface{}, w io.Writer)

DrawRow is called by the framework to draw a row of the table.

func (*Table) DrawTag

func (t *Table) DrawTag(ctx context.Context, w io.Writer)

DrawTag is called by the framework to draw the table. The Table overrides this to call into the DataProvider to load the table's data into memory just before drawing. The data will be unloaded after drawing.

func (*Table) DrawingAttributes

func (t *Table) DrawingAttributes(ctx context.Context) html5tag.Attributes

DrawingAttributes is an override to add attributes to the table, including not showing the table at all if there is no data to show. This will hide header and footer cells and potentially the outline of the table when there is no data in the table.

func (*Table) FooterCellDrawingInfo

func (t *Table) FooterCellDrawingInfo(ctx context.Context, col ColumnI, rowNum int, colNum int) (cellHtml string, cellAttributes html5tag.Attributes)

FooterCellDrawingInfo is called internally to provide the info for each header cell drawn. Subclasses can override this.

func (*Table) FooterRowAttributes

func (t *Table) FooterRowAttributes(row int) html5tag.Attributes

FooterRowAttributes is called internally to get the attributes for the tr tags in footer rows.

func (*Table) FooterRowCount

func (t *Table) FooterRowCount() int

func (*Table) GetColumn

func (t *Table) GetColumn(loc int) ColumnI

GetColumn returns the column at the given location

func (*Table) GetColumnByID

func (t *Table) GetColumnByID(id string) ColumnI

GetColumnByID returns the column with the given id.

func (*Table) GetColumnByTitle

func (t *Table) GetColumnByTitle(title string) ColumnI

GetColumnByTitle returns the column with the given title.

func (*Table) HeaderCellDrawingInfo

func (t *Table) HeaderCellDrawingInfo(ctx context.Context, col ColumnI, rowNum int, colNum int) (cellHtml string, cellAttributes html5tag.Attributes)

HeaderCellDrawingInfo is called internally to provide the info for each header cell drawn. Subclasses can override this.

func (*Table) HeaderRowAttributes

func (t *Table) HeaderRowAttributes(row int) html5tag.Attributes

HeaderRowAttributes is called internally to get the attributes for the tr tags in header rows.

func (*Table) HeaderRowCount

func (t *Table) HeaderRowCount() int

func (*Table) HideColumns

func (t *Table) HideColumns()

HideColumns hides all the columns, keeping them in the column list, but causing them not to draw.

func (*Table) HideIfEmpty

func (t *Table) HideIfEmpty() bool

HideIfEmpty returns true if the table will be completely hidden if there are no rows.

func (*Table) Init

func (t *Table) Init(self any, parent page.ControlI, id string)

Init is an internal function that enables the object-oriented pattern of calling virtual functions used by the GoRADD controls. You would only call this if you were implementing a "subclass" of the Table. Call it immediately after creating your Table structure, passing the newly created table as "self".

func (*Table) MakeSortable

func (t *Table) MakeSortable() TableI

MakeSortable makes a table sortable. It will attach sortable events and show the header if it's not shown.

func (*Table) MarshalState

func (t *Table) MarshalState(m page.SavedState)

MarshalState is an internal function to save the state of the control

func (*Table) RemoveColumn

func (t *Table) RemoveColumn(loc int)

RemoveColumn removes the column at the given location. If you attempt to remove a column that does not exist, it will panic.

func (*Table) RemoveColumnByID

func (t *Table) RemoveColumnByID(id string)

RemoveColumnByID removes the column with the given id. If the column does not exist, nothing will change.

func (*Table) RemoveColumnByTitle

func (t *Table) RemoveColumnByTitle(title string)

RemoveColumnByTitle removes the given column with the given title. If the column does not exist, nothing will change.

func (*Table) Restore

func (t *Table) Restore()

func (*Table) RowAttributes

func (t *Table) RowAttributes(row int, data interface{}) html5tag.Attributes

RowAttributes is used internally to return the attributes for the tr tag of a data row.

func (*Table) RowStyler

func (t *Table) RowStyler() RowAttributer

RowStyler is called by the framework to get the attributes used for every row of the table.

func (*Table) Serialize

func (t *Table) Serialize(e page.Encoder)

func (*Table) SetCaption

func (t *Table) SetCaption(caption interface{}) TableI

SetCaption sets the caption of the table. The default Table permits a caption to be either a string, or a GoRADD control.

func (*Table) SetFooterRowCount

func (t *Table) SetFooterRowCount(count int) TableI

SetFooterRowCount sets the number of footer rows shown. Each column will be asked to draw this number of footer rows.

func (*Table) SetFooterRowStyler

func (t *Table) SetFooterRowStyler(a FooterRowAttributer) TableI

SetFooterRowStyler sets a styler that returns attributes to be used on a particular footer row.

func (*Table) SetHeaderRowCount

func (t *Table) SetHeaderRowCount(count int) TableI

SetHeaderRowCount sets the number of header rows shown. Each column will be asked to draw this number of header rows.

func (*Table) SetHeaderRowStyler

func (t *Table) SetHeaderRowStyler(a HeaderRowAttributer) TableI

SetHeaderRowStyler sets a styler that returns attributes to be used on a particular header row.

func (*Table) SetHideIfEmpty

func (t *Table) SetHideIfEmpty(h bool) TableI

SetHideIfEmpty will tell the table to not draw at all if there is no data in the table, vs. drawing a table tag with no rows.

func (*Table) SetRowStyler

func (t *Table) SetRowStyler(a RowAttributer) TableI

SetRowStyler sets a styler that returns attributes to be used on a particular row.

func (*Table) SetSortColumnsByID

func (t *Table) SetSortColumnsByID(ids ...string)

SetSortColumnsByID sets the order of the sort column list by id.

The specified columns will be set to sorting descended, and all other columns will be set to not be sorting at all.

The columns specified must be sortable.

func (*Table) SetSortHistoryLimit

func (t *Table) SetSortHistoryLimit(n int) TableI

SetSortHistoryLimit sets the number of columns that the table will remember for the sort history. It defaults to 1, meaning it will remember only the current column. Setting it more than 1 will let the system report back on secondary sort columns that the user chose. For example, if the user clicks to sort a first name column, and then a last name column, it will let you know to sort by last name, and then first name.

func (*Table) SetSortIconHtml

func (t *Table) SetSortIconHtml(sortable string, asc string, desc string)

SetSortIconHtml set the html used to draw the sort icons. If a string is blank, it will not be changed. Use the following for font awesome icons

`<i class="fa fa-sort fa-lg"></i>`
`<i class="fa fa-sort-asc fa-lg"></i>`
`<i class="fa fa-sort-desc fa-lg"></i>`

func (*Table) ShowColumns

func (t *Table) ShowColumns()

ShowColumns sets all the columns to be shown.

func (*Table) SortColumns

func (t *Table) SortColumns() (ret []ColumnI)

SortColumns returns a slice of columns in sort order

func (*Table) SortIconHtml

func (t *Table) SortIconHtml(c ColumnI) string

SortIconHtml returns the html used to draw the sort icon

func (*Table) UnmarshalState

func (t *Table) UnmarshalState(m page.SavedState)

UnmarshalState is an internal function to restore the state of the control

func (*Table) UpdateFormValues

func (t *Table) UpdateFormValues(ctx context.Context)

UpdateFormValues is used by the framework to cause the control to retrieve its values from the form

type TableCreator

type TableCreator struct {
	// ID is the control id
	ID string
	// Caption is the content of the caption tag, and can either be a string, or a data pager
	Caption interface{}
	// HideIfEmpty will hide the table completely if it has no data. Otherwise, the table and headers will be shown, but no data rows
	HideIfEmpty bool
	// HeaderRowCount is the number of header rows. You must set this to at least 1 to show header rows.
	HeaderRowCount int
	// FooterRowCount is the number of footer rows.
	FooterRowCount int
	// RowStyler returns the attributes to be used in a cell.
	RowStyler RowAttributer
	// RowStylerID is a control id for the control that will be the RowStyler of the table.
	RowStylerID string
	// HeaderRowStyler returns the attributes to be used in a header cell.
	HeaderRowStyler HeaderRowAttributer
	// HeaderRowStylerID is a control id for the control that will be the HeaderRowStyler of the table.
	HeaderRowStylerID string
	// FooterRowStyler returns the attributes to be used in a footer cell. It can be either a control id or a FooterRowAttributer.
	FooterRowStyler FooterRowAttributer
	// FooterRowStylerID is a control id for the control that will be the FooterRowStyler of the table.
	FooterRowStylerID string
	// Columns are the column creators that will add columns to the table
	Columns []ColumnCreator
	// DataProvider is the control that will dynamically provide the data for the list and that implements the DataBinder interface.
	DataProvider control2.DataBinder
	// DataProviderID is the id of a control that will dynamically provide the data for the list and that implements the DataBinder interface.
	DataProviderID string
	// Data is the actual data for the table, and should be a slice of objects
	Data interface{}
	// Sortable will make the table sortable
	Sortable bool
	// SortHistoryLimit will set how many columns deep we will remember the sorting for multi-level sorts
	SortHistoryLimit int
	// SortableIconHtml will set the html used to draw the icon indicating that a column is sortable. Can also be set globally using SortButtonHtmlGetter
	SortableIconHtml string
	// SortAscIconHtml will set the html used to draw the icon indicating that a column is sorted in ascending order
	SortAscIconHtml string
	// SortDescIconHtml will set the html used to draw the icon indicating that a column is sorted in descending order
	SortDescIconHtml string
	// SortColumnIDs is a list of column ids that will be used to specify the initial sort order
	SortColumnIDs []string
	// OnCellClick is the action to take when a cell is clicked.
	OnCellClick    action.ActionI
	ControlOptions page.ControlOptions
}

TableCreator is the initialization structure for declarative creation of tables

func (TableCreator) Create

func (c TableCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI

Create is called by the framework to create a new control from the Creator. You do not normally need to call this.

func (TableCreator) Init

func (c TableCreator) Init(ctx context.Context, ctrl TableI)

Init is called by implementations of Tables to initialize a base control.

type TableI

type TableI interface {
	page.ControlI
	control2.DataManagerI
	SetCaption(interface{}) TableI
	DrawCaption(context.Context, io.Writer)
	HeaderRowAttributes(row int) html5tag.Attributes
	FooterRowAttributes(row int) html5tag.Attributes
	RowAttributes(row int, data interface{}) html5tag.Attributes
	HeaderCellDrawingInfo(ctx context.Context, col ColumnI, rowNum int, colNum int) (cellHtml string, cellAttributes html5tag.Attributes)
	FooterCellDrawingInfo(ctx context.Context, col ColumnI, rowNum int, colNum int) (cellHtml string, cellAttributes html5tag.Attributes)
	SetHideIfEmpty(h bool) TableI
	SetHeaderRowCount(count int) TableI
	SetFooterRowCount(count int) TableI
	SetRowStyler(a RowAttributer) TableI
	SetHeaderRowStyler(a HeaderRowAttributer) TableI
	SetFooterRowStyler(a FooterRowAttributer) TableI
	AddColumnAt(column ColumnI, loc int)
	AddColumn(column ColumnI) ColumnI
	GetColumn(loc int) ColumnI
	GetColumnByID(id string) ColumnI
	GetColumnByTitle(title string) ColumnI
	RemoveColumn(loc int)
	RemoveColumnByID(id string)
	RemoveColumnByTitle(title string)
	ClearColumns()
	HideColumns()
	ShowColumns()
	MakeSortable() TableI
	SetSortHistoryLimit(n int) TableI
	SortIconHtml(c ColumnI) string
	SetSortIconHtml(sortable string, asc string, desc string)
	DrawRow(ctx context.Context, row int, data interface{}, w io.Writer)
	SetSortColumnsByID(ids ...string)
}

TableI is the table interface that lets you create a "subclass" of the Table object. The functions defined here are hooks that you can implement in your subclass.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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