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.
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.
- Down and Up : next/previous line
- Left and Right : next/previous column.
When a folder row is selected, open/close the folder - Tab and Shift+Tab : next/previous column, then next/previous line
- Home and End : first/last column
- Ctrl+Home and Ctrl+End : first/last line
- Page Down and Page Up : scroll down/up one page.
Use Shift, Ctrl or Ctrl+Shift to move faster. - Ins : insert a row before selected, if
Grid.IsAllowInsertRow = true
(not yet functionnal) - + : add a row at the end, if
Grid.IsAllowInsertRow = true
(not yet functionnal) - Del : remove selected row, if
Grid.IsAllowDeleteRow = true
(not yet functionnal)
Summary
Field | Type | Summary |
---|---|---|
Columns | Map | Map of column definitions. |
EvenBackgroundColor | string | Background color for even lines, in RRGGBB format. |
EvenFolderBackgroundColor | string | Background color for even folder lines, in RRGGBB format. |
FolderSeparator | string | In folder view, separator between sub-folders, default is "/" |
GridColor | string | Grid lines color in RRGGBB format. |
HeadersBackColor | string | Background color for headers, in RRGGBB format. |
HeadersGridColor | string | Grid line color in headers, in RRGGBB format. |
HeadersSelectedColor | string | Color in headers to locate selected cell, in RRGGBBAA format. |
HeadersTextColor | string | Text color in headers, in RRGGBB format. |
IsFolderSort | boolean | If folder view, force the sort of folders, true by default. |
IsFolderView | boolean | Display as a folder view, like File>New, Staff>Add... |
IsHighlightFullRow | boolean | Always highlight the entire selected row? |
IsShowColSeparators | boolean | Show column separator lines? |
IsShowColumnCaptions | boolean | Show caption in headers? |
IsShowHeader | boolean | Show header row? |
IsShowRowNumbers | boolean | Show row numbers (default is true )? |
IsShowRowSeperators | boolean | Show row separator lines? |
OddBackgroundColor | string | Background color for odd lines, in RRGGBB format. |
OddFolderBackgroundColor | string | Background color for odd folder lines, in RRGGBB format. |
RowHeight | number | Row height in pixels. |
Rows | table | Data in rows of the grid. |
SelectedColor | string | Color of selected cell in RRGGBB format, default is Application.RGBSelectColor with some transparency. |
SortCriteria | table | Criteria for sorting data in the Grid. |
Textcolor | string | Text color, in RRGGBB format. |
Return type | Function and summary |
---|---|
GridColumn | 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 |
Map | AddRow(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. | |
boolean | Filter(Dialog dialot, Grid grid, int rowIndex, Map rowMap) Filter the rows to show in the grid. |
GridColumn | GetColumn(Grid grid, string idOrCaption) Return the requested column from its caption or ID (excel like letter "A", "B"...). |
Map | GetSelectedRow(Grid grid) Return the select row's Map of columnName to value. |
table | GetVisibleRows(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 | |
boolean | SearchRow(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.SelectedColor
Color of selected cell in RRGGBB format, default is Application.RGBSelectColor
with some transparency.
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.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.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.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...
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.
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
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..
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
Parameter | Type | Default | Description |
---|---|---|---|
dlg | Dialog | Reference to the MyrScript's dialog | |
grid | Grid | Reference to the grid | |
caption | string | 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. | |
defaultValue | string | Value for new row | |
valueType | string | "string" | "string", "number" or "table" . You can customize visible data (picture, formatted number), but the value must be a string or a number. |
possibleValues | table | nil | Force value to one of the table. Not yet implemented. @todo Click on the cell to pick a value, Up/Down to rotate between values? |
width | number | 10 |
|
horizontalAlign | int | ALIGN_LEFT | See constants in MSDefine. If nil , set ALIGN_RIGHT for numbers, ALIGN_LEFT for strings. |
visible | boolean | true | Visible (true ) or hidden (false )? |
autoTranslate | boolean | false | Translate 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.
Parameter | Type | Default | Description |
---|---|---|---|
grid | Grid | grid | |
idOrCaption | string | 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
Parameter | Type | Default | Description |
---|---|---|---|
dlg | Dialog | Reference to the MyrScript's Dialog | |
grid | Grid | 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. | ||
pos | int | nil | Position 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
Grid.GetSelectedRow(Grid grid)
Return the select row's Map of columnName to value.
Parameter | Type | Default | Description |
---|---|---|---|
grid | Grid | 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.
Grid.GetVisibleRows(Dialog dlg, Grid grid)
Return all visible rows after applying filter and sort.
Ignore folder, consider all folder as open.
- 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.
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.
Grid.WriteCSVFile(Dialog dlg, Grid grid, string csvFileName)
Save grid data in CSV file.
Not fully tested, export all rows, even hidden ones.
Grid.SearchRow(Dialog dlg, Grid grid, term, function evalFunc)
Search first row from the top where evalFunction returns true
and select the row.
Parameter | Type | Default | Description |
---|---|---|---|
dlg | Dialog | dlg | |
grid | Grid | grid | |
term | The Term to search that will be passed in argument #4 to evalFunc. No type restrictipn, nil , string, table or something else. | ||
evalFunc | function | A function called for each row. It must accepts 3 arguments: . 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.
Parameter | Type | Default | Description |
---|---|---|---|
dialot | Dialog | dialot | |
grid | Grid | grid | |
rowIndex | int | rowIndex | |
rowMap | Map | rowMap |
- Return
- boolean:
true
to show the row,false
ornil
to hide it.
Grid.MoveToFirstRow(Dialog dlg, Grid grid)
Move selection to first row if exists.
Grid.MoveToLastRow(Dialog dlg, Grid grid)
Move selection to last row if exists.
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.
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.
Grid.MoveToFirstColumn(Dialog, Grid grid)
Move selection to first column
Parameter | Type | Default | Description |
---|---|---|---|
Dialog | dlg | ||
grid | Grid | grid |
Grid.MoveToLastColumn(Dialog dlg, Grid grid)
Move selection to last column.
Grid.MoveToNextColumn(Dialog dlg, Grid grid)
Move selection to next column.
If no column is selected, move to first column.
Grid.MoveToPreviousColumn(Dialog dlg, Grid grid)
Move selection to previous column.
If no column is selected, move to first column.