Table Grid

Grid component, a flexible 2D "table" with customizable events and draw for each column.

A grid has columns with headers and ID that are Excel-like letters "A", "B", ... "Z", "AA"... and rows from 1 to N
This component is navigable by click, scroll, and key strockes.

Grid is a complex Component: it owns same fields than a button and a label, and add more.

Grid, with default parameters

Columns definition

Download this script to fully test Grid features.
This script goes to Script menu > Miscellaneous > Dev tools > SMDialog Grid.
It demonstrates features and you can copy/paste generated code to speed up your script creation.

Navigation

Grid are navigable with keyboard. Test it in the demo script.

See
SMDialog:addGrid

Summary

FieldTypeSummary
ColumnsMapMap of column definitions.
EvenBackgroundColorstringBackground color for even lines, in RRGGBB format.
EvenFolderBackgroundColorstringBackground color for even folder lines, in RRGGBB format.
FolderSeparatorstringIn folder view, separator between sub-folders, default is "/"
GridColorstringGrid lines color in RRGGBB format.
HeadersBackColorstringBackground color for headers, in RRGGBB format.
HeadersGridColorstringGrid line color in headers, in RRGGBB format.
HeadersSelectedColorstringColor in headers to locate selected cell, in RRGGBBAA format.
HeadersTextColorstringText color in headers, in RRGGBB format.
IsFolderSortbooleanIf folder view, force the sort of folders, true by default.
IsFolderViewbooleanDisplay as a folder view, like File>New, Staff>Add...
IsHighlightFullRowbooleanAlways highlight the entire selected row?
IsShowColSeparatorsbooleanShow column separator lines?
IsShowColumnCaptionsbooleanShow caption in headers?
IsShowHeaderbooleanShow header row?
IsShowRowNumbersbooleanShow row numbers (default is true)?
IsShowRowSeperatorsbooleanShow row separator lines?
OddBackgroundColorstringBackground color for odd lines, in RRGGBB format.
OddFolderBackgroundColorstringBackground color for odd folder lines, in RRGGBB format.
RowHeightnumberRow height in pixels.
RowstableData in rows of the grid.
SelectedColorstringColor of selected cell in RRGGBB format, default is Application.RGBSelectColor with some transparency.
SortCriteriatableCriteria for sorting data in the Grid.
TextcolorstringText color, in RRGGBB format.
Return typeFunction and summary
GridColumnAddColumn(Dialog dlg, Grid grid, string caption, string defaultValue, string valueType, table possibleValues, number width, int horizontalAlign, boolean visible, boolean autoTranslate)
Adds a column to the grid
MapAddRow(Dialog dlg, Grid grid, map, int pos)
Insert a map of columnName=>value into a new row of the grid.
 CloseFolder(Dialog dlg, Grid grid, string folderName)
Closes a folder, if IsFolderView is true Sub-folders are shadowed, it means they will show again once folder is open.
booleanFilter(Dialog dialot, Grid grid, int rowIndex, Map rowMap)
Filter the rows to show in the grid.
GridColumnGetColumn(Grid grid, string idOrCaption)
Return the requested column from its caption or ID (excel like letter "A", "B"...).
MapGetSelectedRow(Grid grid)
Return the select row's Map of columnName to value.
tableGetVisibleRows(Dialog dlg, Grid grid)
Return all visible rows after applying filter and sort.
 MoveToFirstColumn(Dialog, Grid grid)
Move selection to first column
 MoveToFirstRow(Dialog dlg, Grid grid)
Move selection to first row if exists.
 MoveToLastColumn(Dialog dlg, Grid grid)
Move selection to last column.
 MoveToLastRow(Dialog dlg, Grid grid)
Move selection to last row if exists.
 MoveToNextColumn(Dialog dlg, Grid grid)
Move selection to next column.
 MoveToNextRow(Dialog dlg, Grid grid, int amount)
Move selection to next row, if exists.
 MoveToPreviousColumn(Dialog dlg, Grid grid)
Move selection to previous column.
 MoveToPreviousRow(Dialog dlg, Grid grid, int amount)
Move selection to previous row, if exists.
 OnRowSelect(Dialog dlg, Grid grid, int rowIndex, Map rowMap)
Function fired when row selection changes..
 OpenFolder(Dialog dlg, Grid grid, string folderName)
Opens a folder, if IsFolderView is true All parents folders are open.
 RemoveAllRows(Dialog dlg, Grid grid)
Remove all rows from the grid
booleanSearchRow(Dialog dlg, Grid grid, term, function evalFunc)
Search first row from the top where evalFunction returns true and select the row.
 SetSelectedRow(Dialog dlg, Grid grid, int rowIndex)
Select a row of the grid.
 WriteCSVFile(Dialog dlg, Grid grid, string csvFileName)
Save grid data in CSV file.

Fields

string Grid.GridColor

Grid lines color in RRGGBB format.

Default="BBBBBB" (light grey)

string Grid.SelectedColor

Color of selected cell in RRGGBB format, default is Application.RGBSelectColor with some transparency.

string Grid.Textcolor

Text color, in RRGGBB format.

Default is "000000" (black)

string Grid.OddBackgroundColor

Background color for odd lines, in RRGGBB format.

Default is "FEFFFF" (white)

string Grid.EvenBackgroundColor

Background color for even lines, in RRGGBB format.

Default is "EDF2FF" (light grey)

string Grid.OddFolderBackgroundColor

Background color for odd folder lines, in RRGGBB format.

Default is "FFCC90" (yellow)

string Grid.EvenFolderBackgroundColor

Background color for even folder lines, in RRGGBB format.

Default is "FFC060" (yellow)

string Grid.HeadersGridColor

Grid line color in headers, in RRGGBB format.

Default is "000000" (black)

string Grid.HeadersTextColor

Text color in headers, in RRGGBB format.

Default is "000000" (black)

string Grid.HeadersBackColor

Background color for headers, in RRGGBB format.

Default is "BBBBBB" (light grey)

string Grid.HeadersSelectedColor

Color in headers to locate selected cell, in RRGGBBAA format.

Default is Application.RGBSelectColor.."80".

boolean Grid.IsShowHeader

Show header row?

Default is true.

boolean Grid.IsShowColumnCaptions

Show caption in headers?

If true (default), show column caption, else, show Excel-like alpha "A", "B", ... "Z", "AA"...

boolean Grid.IsShowRowNumbers

Show row numbers (default is true)?

If true an extra column is visible on the le left with row numbers.

boolean Grid.IsShowColSeparators

Show column separator lines?

Default is true

boolean Grid.IsShowRowSeperators

Show row separator lines?

Default is true

number Grid.RowHeight

Row height in pixels.

Default is calculated.

boolean Grid.IsHighlightFullRow

Always highlight the entire selected row?

False by default, means only highlight the cell at selected column of selected row.

boolean Grid.IsFolderView

Display as a folder view, like File>New, Staff>Add...

? First column is like a folder/subfolder name (separated with a slash), this will then create folder view, open/close folder on click...

Grid with folders

boolean Grid.IsFolderSort

If folder view, force the sort of folders, true by default.

Turn it to false if datas are already sorted, to speed up display.

string Grid.FolderSeparator

In folder view, separator between sub-folders, default is "/"

Map Grid.Columns

Map of column definitions.

Keys are the ID (alpha "A", "B", ... "Z", "AA"), values are GridColumn table.

See
Grid.AddColumn
Grid.GetColumn
GridColumn

table Grid.Rows

Data in rows of the grid.

Array (table) of Map.
Rows[1] is the first row.
Rows[1].get("columnName") returns value in column having columnName as header for the first row

See
Grid.AddRow
Grid.GetSelectedRow
Grid.SetSelectedRow

table Grid.SortCriteria

Criteria for sorting data in the Grid.

2D table in the form {{"ColumnName 1", n}, {"ColumnName 2", n}} where n = 1 for ascending order, n = -1 for descending order.
nil or empty table means no sorting. This is lighter and faster, so if inserted rows can be sorted before insertion, do this. If rows content may change, then it's easier to use SortCriteria.

Functions

Grid.OnRowSelect(Dialog dlg, Grid grid, int rowIndex, Map rowMap)

Function fired when row selection changes..

ParameterTypeDefaultDescription
dlgDialog The MyrScript Dialog object
gridGrid The grid
rowIndexint The selected row, 0 = no row selected.
rowMapMap The data of the selected row, nil if no row selected.

Grid.AddColumn(Dialog dlg, Grid grid, string caption, string defaultValue, string valueType, table possibleValues, number width, int horizontalAlign, boolean visible, boolean autoTranslate)

Adds a column to the grid

ParameterTypeDefaultDescription
dlgDialog Reference to the MyrScript's dialog
gridGrid Reference to the grid
captionstring Header of the column, name. This may be used as identifier to retrive datas.
Note: if your script is internationalized, caption may change for each language, original caption still work to retrieve datas.
defaultValuestring Value for new row
valueTypestring"string""string", "number" or "table". You can customize visible data (picture, formatted number), but the value must be a string or a number.
possibleValuestablenilForce value to one of the table. Not yet implemented. @todo Click on the cell to pick a value, Up/Down to rotate between values?
widthnumber10
  • If positive, width will be adjusted in proportion of sum of column widths, If "A" column's width is 1 and "B"'s is 3, then "B" will be 3 times larger than "A".
  • If negative, width is fixed in pixels, -10 means 10 pixel large, useful for image column.
horizontalAlignintALIGN_LEFTSee constants in MSDefine. If nil, set ALIGN_RIGHT for numbers, ALIGN_LEFT for strings.
visiblebooleantrueVisible (true) or hidden (false)?
autoTranslatebooleanfalseTranslate automatically column content?
Return
GridColumn: The created grid column as table object with properties and functions. You can add event, see GridColumn.
Errors
if type of defaultValue is not equal to valueType
in case of duplicate captions
See
Grid.Columns
Grid.GetColumn
GridColumn

Grid.GetColumn(Grid grid, string idOrCaption)

Return the requested column from its caption or ID (excel like letter "A", "B"...).

Grid.Columns can easily find column by ID, but not by caption. This function helps you.

ParameterTypeDefaultDescription
gridGrid grid
idOrCaptionstring idOrCaption
Return
GridColumn: Found column.
Errors
If idOrCaption is not a string or an empty one.
If column is not found
See
Grid.Columns
Grid.AddColumn
GridColumn

Grid.AddRow(Dialog dlg, Grid grid, map, int pos)

Insert a map of columnName=>value into a new row of the grid.

If pos is not nil, this will insert at position pos, else this will insert after the last row

ParameterTypeDefaultDescription
dlgDialog Reference to the MyrScript's Dialog
gridGrid The grid object
map  Map of columnNames to values, table is also accepted: t["ColumnCaption"]="value" or t.ColumnName="Value" are preferred, but t[1]="&" t[2]="b" also works.
posintnilPosition of the inserted row, nil = after last
Return
Map: Map of column names to values
See
Grid.GetSelectedRow
Grid.SetSelectedRow
Grid.RemoveAllRows
Grid.Rows

Grid.RemoveAllRows(Dialog dlg, Grid grid)

Remove all rows from the grid

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid

Grid.GetSelectedRow(Grid grid)

Return the select row's Map of columnName to value.

ParameterTypeDefaultDescription
gridGrid grid
Return
Map: The row's Map, or nil if no row is selected
See
Grid.SetSelectedRow
Grid.Rows

Grid.SetSelectedRow(Dialog dlg, Grid grid, int rowIndex)

Select a row of the grid.

This function doesn't fire the OnRowSelect event, if implemented, but it'll be fired as soon as possible on idle.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid The grid
rowIndexint The index of the the row to select, 0=no selection. Value is protected between 1 and row count.
See
Grid.GetSelectedRow
Grid.Rows

Grid.GetVisibleRows(Dialog dlg, Grid grid)

Return all visible rows after applying filter and sort.

Ignore folder, consider all folder as open.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
Return
table: Listof rows, each one is a Map.
See
Grid.Filter
Grid.SortCriteria

Grid.OpenFolder(Dialog dlg, Grid grid, string folderName)

Opens a folder, if IsFolderView is true All parents folders are open.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
folderNamestring folderName

Grid.CloseFolder(Dialog dlg, Grid grid, string folderName)

Closes a folder, if IsFolderView is true Sub-folders are shadowed, it means they will show again once folder is open.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
folderNamestring folderName

Grid.WriteCSVFile(Dialog dlg, Grid grid, string csvFileName)

Save grid data in CSV file.


Not fully tested, export all rows, even hidden ones.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
csvFileNamestring csvFileName

Grid.SearchRow(Dialog dlg, Grid grid, term, function evalFunc)

Search first row from the top where evalFunction returns true and select the row.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
term  The Term to search that will be passed in argument #4 to evalFunc.
No type restrictipn, nil, string, table or something else.
evalFuncfunction A function called for each row. It must accepts 3 arguments:
  • The Dialog
  • the Grid
  • a Map representing the row of the grid
  • term value
. evalFunc must return true to select the row and stop the search.
Return
boolean: Returns true if a row was found, false if not. If no row was found, the selection is not moved.
Example


 local func = function(dlg, grid, rowMap, term)
      return rowMap:get("My column name")==term
    end
  local myTerm = tostring(123) -- if number, convert it to string once
  myGrid.SearchRow(dlg, myGrid, myTerm, func)

Grid.Filter(Dialog dialot, Grid grid, int rowIndex, Map rowMap)

Filter the rows to show in the grid.

For each row, this function may return true to show it, or false to hide it.

ParameterTypeDefaultDescription
dialotDialog dialot
gridGrid grid
rowIndexint rowIndex
rowMapMap rowMap
Return
boolean: true to show the row, false or nil to hide it.

Grid.MoveToFirstRow(Dialog dlg, Grid grid)

Move selection to first row if exists.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid

Grid.MoveToLastRow(Dialog dlg, Grid grid)

Move selection to last row if exists.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid

Grid.MoveToNextRow(Dialog dlg, Grid grid, int amount)

Move selection to next row, if exists.

If no row is selected, move to first row, if exists.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
amountint1Value greater than 1 to jump quickly amount next rows.

Grid.MoveToPreviousRow(Dialog dlg, Grid grid, int amount)

Move selection to previous row, if exists.

If no row is selected, move to first row, if exists.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid
amountint1Value greater than 1 to jump quickly amount previous rows.

Grid.MoveToFirstColumn(Dialog, Grid grid)

Move selection to first column

ParameterTypeDefaultDescription
Dialog  dlg
gridGrid grid

Grid.MoveToLastColumn(Dialog dlg, Grid grid)

Move selection to last column.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid

Grid.MoveToNextColumn(Dialog dlg, Grid grid)

Move selection to next column.

If no column is selected, move to first column.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid

Grid.MoveToPreviousColumn(Dialog dlg, Grid grid)

Move selection to previous column.

If no column is selected, move to first column.

ParameterTypeDefaultDescription
dlgDialog dlg
gridGrid grid