TableView

The TableView element implements a table. To create a TableView, the function is used:

func NewTableView(session Session, params Params) TableView

"content" property

The "content" property defines the content of the table.To describe the content, you need to implement the TableAdapter interface declared as

type TableAdapter interface {
	RowCount() int
	ColumnCount() int
	Cell(row, column int) any
}

where RowCount() and ColumnCount() functions must return the number of rows and columns in the table;Cell(row, column int) returns the contents of a table cell. The Cell() function can return elements of the following types:

  • string
  • rune
  • float32, float64
  • integer values: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64
  • bool
  • rui.Color
  • rui.View
  • fmt.Stringer
  • rui.VerticalTableJoin, rui.HorizontalTableJoin

The "content" property can also be assigned the following data types

  • TableAdapter
  • [][]any
  • [][]string

[][]any and [][]string are converted to a TableAdapter when assigned.

If the elements of the table change during operation, then to update the contents of the table,you must call one of the two methods of the TableView interface

  • ReloadTableData()
  • ReloadCell(row, column int)

The ReloadTableData method updates the entire table, while ReloadCell updates the contents of only a specific table cell.Global functions can be used to call the ReloadTableData and ReloadCell methods

func ReloadTableViewData(view View, subviewID ...string) bool
func ReloadTableViewCell(row, column int, view View, subviewID ...string) bool

"cell-style" property

The "cell-style" property (CellStyle constant) is used to customize the appearance of a table cell.Only an implementation of the TableCellStyle interface can be assigned to this property.

type TableCellStyle interface {
	CellStyle(row, column int) Params
}

This interface contains only one CellStyle function that returns the styling parameters for a given table cell.Any properties of the View interface can be used. For example

func (style *myTableCellStyle) CellStyle(row, column int) rui.Params {
	if row == 0 {
		return rui.Params {
			rui.BackgroundColor: rui.Gray,
			rui.Italic:          true,
		}
	}
	return nil
}

If you don't need to change the appearance of a cell, you can return nil for it.

"row-span" and "column-span" properties

In addition to the properties of the View interface, the CellStyle function can return two more properties of type int:"row-span" (RowSpan constant) and "column-span" (ColumnSpan constant).These properties are used to combine table cells.

The "row-span" property specifies how many cells to merge vertically, and the "column-span" property - horizontally. For example

func (style *myTableCellStyle) CellStyle(row, column int) rui.Params {
	if row == 0 && column == 0 {
		return rui.Params { rui.RowSpan: 2 }
	}
	if row == 0 && column == 1 {
		return rui.Params { rui.ColumnSpan: 2 }
	}
	return nil
}

In this case, the table will look like this

|------+----------------|
|      |                |
|      +-------+--------|
|      |       |        |
|------+-------+--------|

If [][]any is used as the value of the "content" property, then empty structures are used to merge cells

type VerticalTableJoin struct {
}
type HorizontalTableJoin struct {
}

These structures attach the cell to the top/left, respectively. The description of the above table will be as follows

content := [][]any {
	{"", "", rui.HorizontalTableJoin{}},
	{rui.VerticalTableJoin{}, "", ""},
}

"row-style" property

The "row-style" property (RowStyle constant) is used to customize the appearance of a table row.This property can be assigned either an implementation of the TableRowStyle interface or []Params.TableRowStyle is declared as

type TableRowStyle interface {
	RowStyle(row int) Params
}

The RowStyle function returns parameters that apply to the entire row of the table.The "row-style" property has a lower priority than the "cell-style" property, i.e.properties set in "cell-style" will be used instead of those set in "row-style"

"column-style" property

The "column-style" property (ColumnStyle constant) is used to customize the appearance of a table column.This property can be assigned either an implementation of the TableColumnStyle interface or []Params.TableColumnStyle is declared as

type TableColumnStyle interface {
	ColumnStyle(column int) Params
}

The ColumnStyle function returns the parameters applied to the entire column of the table.The "column-style" property has a lower precedence over the "cell-style" and "row-style" properties.

"head-height" and "head-style" properties

The table can have a header.The "head-height" int property (constant HeadHeight) indicates how many first rows of the table form the header.The "head-style" property (constant HeadStyle) sets the style of the heading. The "head-style" property can beassigned, value of type:

  • string - style name;
  • []Params - enumeration of header properties.

"foot-height" and "foot-style" properties

The table can have finalizing lines at the end (footer). For example, the "total" line.The "foot-height" int property (the FootHeight constant) indicates the number of these footer lines.The "foot-style" property (constant FootStyle) sets footer style.The values for the "foot-style" property are the same as for the "head-style" property.

"cell-padding" property

The "cell-padding" BoundsProperty property (CellPadding constant) sets the padding from the cell borders to the content.This property is equivalent to

func (style *myTableCellStyle) CellStyle(row, column int) rui.Params {
	return rui.Params { rui.Padding:  }
}

And it was introduced for convenience, so that you do not have to write an adapter to set indents.The cell-padding property has a lower priority than the "cell-style" property.

"cell-padding" can also be used when setting parameters in the "row-style", "column-style", "foot-style", and "head-style" properties

"cell-border" property

The "cell-border" property (CellBorder constant) sets the memory for all table cells.This property is equivalent to

func (style *myTableCellStyle) CellStyle(row, column int) rui.Params {
	return rui.Params { rui.Border:  }
}

And it was introduced for convenience, so that it is not necessary to write an adapter for the frame.The "cell-border" property has a lower precedence over the "cell-style" property.

"cell-border" can also be used when setting parameters in properties"row-style", "column-style", "foot-style" and "head-style"

"table-vertical-align" property

The "table-vertical-align" int property (TableVerticalAlign constant) specifiesthe vertical alignment of data within a table cell. Valid values:

Value Constant Name Alignment
0 TopAlign "top" Top alignment
1 BottomAlign "bottom" Bottom alignment
2 CenterAlign "center" Center alignment
3, 4 BaselineAlign "baseline" Baseline alignment

For horizontal alignment, use the "text-align" property

You can get the value of this property using the function

func GetTableVerticalAlign(view View, subviewID ...string) int

"selection-mode" property

The "selection-mode" property (SelectionMode constant) of the int type determines the mode of selection (highlighting) of table elements. Available modes:

  • NoneSelection (0). Default mode. In this mode, you cannot select table elements. The table cannot receive input focus.
  • CellSelection (1). In this mode, one table cell can be selected (highlighted).The cell is selected interactively using the mouse or keyboard (using the cursor keys).In this mode, the table can receive input focus. In this mode, the table generates two types of events: "table-cell-selected" and "table-cell-clicked" (see below).
  • RowSelection (2). In this mode, only the entire table row can be selected (highlighted).In this mode, the table is similar to a ListView. The row is selected interactivelywith the mouse or keyboard (using the cursor keys). In this mode, the table can receive input focus.In this mode, the table generates two types of events: "table-row-selected" and "table-row-clicked" (see below).

You can get the value of this property using the function

func GetSelectionMode(view View, subviewID ...string) int

"current" property

The "current" property (Current constant) sets the coordinates of the selected cell/row as a structure

type CellIndex struct {
	Row, Column int
}

If the cell is not selected, then the values of the Row and Column fields will be less than 0.

In RowSelection mode, the value of the Column field is ignored. Also in this mode,the "current" property can be assigned a value of type int (row index).

You can get the value of this property using the function

func GetTableCurrent(view View, subviewID ...string) CellIndex

"allow-selection" property

By default, you can select any cell/row of the table. However, it is often necessary to disable the selection of certain elements. The "selection-mode" property (SelectionMode constant) allows you to set such a rule.

In CellSelection mode, this property is assigned the implementation of the interface

type TableAllowCellSelection interface {
	AllowCellSelection(row, column int) bool
}

and in RowSelection mode this property is assigned the implementation of the interface

type TableAllowRowSelection interface {
	AllowRowSelection(row int) bool
}

The AllowCellSelection/AllowRowSelection function must return "true"if the cell/row can be selected and "false" if the cell/row cannot be selected.

"table-cell-selected" and "table-cell-clicked" events

The "table-cell-selected" event is fired in CellSelection mode when the user has selecteda table cell with the mouse or keyboard.

The "table-cell-clicked" event occurs if the user clicks on a table cell (and if it is not selected,the "table-cell-selected" event occurs first) or presses the Enter or Space key.

The main listener for these events has the following format:

func(TableView, int, int)

where the second argument is the cell row index, the third argument is the column index

You can also use a listener in the following format:

func(int, int)

"table-row-selected" and "table-row-clicked" events

The "table-row-selected" event is fired in RowSelection mode when the user has selecteda table row with the mouse or keyboard.

The "table-row-clicked" event occurs if the user clicks on a table row (if it is not selected,the "table-row-selected" event fires first) or presses the Enter or Space key.

The main listener for these events has the following format:

func(TableView, int)

where the second argument is the row index.

You can also use a listener in the following format:

func(int)