Table Grid

Grid component, a flexible "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 based on Button: it owns Button fields and add more.

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.
IsFolderViewboolDisplay as a folder view, like File>New, Staff>Add...
IsHighlightFullRowboolAlways highlight the entire selected row?
IsShowColSeparatorsboolShow column separator lines?
IsShowColumnCaptionsboolShow caption in headers?
IsShowHeaderboolShow header row?
IsShowRowNumbersboolShow row numbers (default is true)?
IsShowRowSeperatorsboolShow 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
SortCriteriatableCriteria for sorting data in the Grid.
TextcolorstringText color, in RRGGBB format.
Return typeFunction and summary
GridColumnAddColumn(Dialog dialog, Grid grid, string caption, string defaultValue, string valueType, table possibleValues, number width, int horizontalAlign, bool visible, bool autoTranslate)
Adds a column to the grid
 AddRow(Dialog dialog, Grid grid, map, int pos)
Insert a map of columnName=>value into a new row of the grid.
 CloseFolder(Dialog dialog, 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.
boolFilter(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.
 MoveToFirstColumn(Dialog, Grid grid)
Move selection to first column
 MoveToFirstRow(Dialog dialog, Grid grid)
Move selection to first row if exists.
 MoveToLastColumn(Dialog dialog, Grid grid)
Move selection to last column.
 MoveToLastRow(Dialog dialog, Grid grid)
Move selection to last row if exists.
 MoveToNextColumn(Dialog dialog, Grid grid)
Move selection to next column.
 MoveToNextRow(Dialog dialog, Grid grid, int amount)
Move selection to next row, if exists.
 MoveToPreviousColumn(Dialog dialog, Grid grid)
Move selection to previous column.
 MoveToPreviousRow(Dialog dialog, Grid grid, int amount)
Move selection to previous row, if exists.
 OnRowSelect(Dialog dialog, Grid grid, int rowIndex, Map rowMap)
Function fired when row selection changes..
 OpenFolder(Dialog dialog, Grid grid, string folderName)
Opens a folder, if IsFolderView is true All parents folders are open.
 RemoveAllRows(Dialog dialog, Grid grid)
Remove all rows from the grid
boolSearchRow(Dialog dialog, Grid grid, term, function evalFunc)
Search first row from the top where evalFunction returns true and select the row.
 SetSelectedRow(Dialog dialog, Grid grid, int rowIndex)
Select a row of the grid.
 WriteCSVFile(Dialog dialog, 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

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 lApplication.RGBSelectColor.."80".

bool Grid.IsShowHeader

Show header row?

Default is true.

bool Grid.IsShowColumnCaptions

Show caption in headers?

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

bool Grid.IsShowRowNumbers

Show row numbers (default is true)?

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

bool Grid.IsShowColSeparators

Show column separator lines?

Default is true

bool Grid.IsShowRowSeperators

Show row separator lines?

Default is true

number Grid.RowHeight

Row height in pixels.

Default is calculated.

bool Grid.IsHighlightFullRow

Always highlight the entire selected row?

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

bool 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...

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 dialog, Grid grid, int rowIndex, Map rowMap)

Function fired when row selection changes..

ParameterTypeDefaultDescription
dialogDialog 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 dialog, Grid grid, string caption, string defaultValue, string valueType, table possibleValues, number width, int horizontalAlign, bool visible, bool autoTranslate)

Adds a column to the grid

ParameterTypeDefaultDescription
dialogDialog Reference to the 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 should still work.
defaultValuestring Value for new row
valueTypestring"string""string" or "number". 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<:kbd>/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.
visiblebooltrueVisible (true) or hidden (false)?
autoTranslateboolfalseTranslate automatically column content?
Return
GridColumn: the table representing the created column properties. 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  
idOrCaptionstring  
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 dialog, 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
dialogDialog Reference to the 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
See
Grid.GetSelectedRow
Grid.SetSelectedRow
Grid.RemoveAllRows
Grid.Rows

Grid.RemoveAllRows(Dialog dialog, Grid grid)

Remove all rows from the grid

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  

Grid.GetSelectedRow(Grid grid)

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

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

Grid.SetSelectedRow(Dialog dialog, 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
dialogDialog  
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.OpenFolder(Dialog dialog, Grid grid, string folderName)

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

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  
folderNamestring  

Grid.CloseFolder(Dialog dialog, 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
dialogDialog  
gridGrid  
folderNamestring  

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

Save grid data in CSV file.


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

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  
csvFileNamestring  

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

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

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  
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
bool: Returns true if a row was found, false if not. If no row was found, the selection is not moved.
Example
local func = function(dialog, grid, rowMap, term)
return rowMap:get("My column name")==term
end
local myTerm = tostring(123) -- if number, convert it to string once
myGrid.SearchRow(dialog, 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  
gridGrid  
rowIndexint  
rowMapMap  
Return
bool: true to show the row, false or nil to hide it.

Grid.MoveToFirstRow(Dialog dialog, Grid grid)

Move selection to first row if exists.

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  

Grid.MoveToLastRow(Dialog dialog, Grid grid)

Move selection to last row if exists.

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  

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

Move selection to next row, if exists.

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

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  
amountint1Value greater than 1 to jump quickly amount next rows.

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

Move selection to previous row, if exists.

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

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  
amountint1Value greater than 1 to jump quickly amount previous rows.

Grid.MoveToFirstColumn(Dialog, Grid grid)

Move selection to first column

ParameterTypeDefaultDescription
Dialog  dialog
gridGrid  

Grid.MoveToLastColumn(Dialog dialog, Grid grid)

Move selection to last column.

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  

Grid.MoveToNextColumn(Dialog dialog, Grid grid)

Move selection to next column.

If no column is selected, move to first column.

ParameterTypeDefaultDescription
dialogDialog  
gridGrid  

Grid.MoveToPreviousColumn(Dialog dialog, Grid grid)

Move selection to previous column.

If no column is selected, move to first column.

ParameterTypeDefaultDescription
dialogDialog  
gridGrid