Table of Contents

Tables

Tables in DGLux5 have named columns and a list of rows. The first column is always row; it holds the row index (0, 1, 2, …) and is renumbered automatically every time the table changes.

Script can work with tables in two ways:

Table functions

The table argument of these functions is usually a table value from the page model, for example @parent.table (the table property of a Table block) or @parent.filter.output.

Writing a table into the Script block

tableSet can also create a table and store it in a property of the Script block itself. Pass the property name as a string, or as @.name. You can then read it back with @.name and bind other blocks to it.

tableSet("t", [[1, "a"], [2, "b"]], ["id", "name"]);
return tableGetRows(@.t);

Output: [[0, 1, a], [1, 2, b]]

tableAddRows, tableRemoveRows, and tableClear can also change such a property; pass it as @.name written directly in the call (not through a variable).

The examples below use this technique so that they run in a Script block on their own. In a real page you would normally pass a Table block's table property instead of @.t.

tableGetRows

tableGetRows(table, [mode], [excludeRow])

Returns the rows of table.

Returns null if table is not a table.

tableSet("t", [[1, "a"], [2, "b"]], ["id", "name"]);
return tableGetRows(@.t, "object", true);

Output: [{id: 1, name: a}, {id: 2, name: b}]

tableGetColumns

tableGetColumns(table)

Returns a list with one object per column. Each object has name, type (such as string, number, bool, time), width, formula, and meta. Returns null if table is not a table.

tableSet("t", [[1, "a"]], ["id", "name"]);
cols = tableGetColumns(@.t);
names = [];
for (i = 0; i < cols.length; i++) {
  names.push(cols[i].name + ":" + cols[i].type);
}
return names;

Output: [row:number, id:string, name:string]

tableSet

tableSet(table, rows, [columns], [serialize])

Replaces all data in table.

Throws an error if table is not a table or rows is not a list.

tableSet("t", [{id: 1, name: "a"}, {name: "b", id: 2}], [{name: "id", type: "number"}, "name"]);
return tableGetRows(@.t);

Output: [[0, 1, a], [1, 2, b]]

To fill a Table block on the page:

tableSet(@parent.table, [["a", "b"], ["c", "d"]], ["col1", "col2"]);

tableAddRows

tableAddRows(table, rows)

Adds rows at the end of table. Each row can be a list in the table's column order, including the row column (so start each row with a placeholder value, which is replaced by the row index), or a map from column names to values.

Throws an error if table is not a table or rows is not a list.

tableSet("t", [[1, "a"]], ["id", "name"]);
tableAddRows(@.t, [["", 2, "b"], ["", 3, "c"]]);
return tableGetRows(@.t);

Output: [[0, 1, a], [1, 2, b], [2, 3, c]]

Rows as maps:

tableSet("t", [[1, "a"]], ["id", "name"]);
tableAddRows(@.t, [{"id": 2, "name": "b"}]);
return tableGetRows(@.t);

Output: [[0, 1, a], [1, 2, b]]

tableRemoveRows

tableRemoveRows(table, start, [count])

Removes count rows (default 1) starting at row index start. Does nothing if start is outside the table. If start + count is past the end, the rows up to the end are removed.

tableSet("t", [[1], [2], [3], [4]], ["v"]);
tableRemoveRows(@.t, 1, 2);
return tableGetRows(@.t);

Output: [[0, 1], [1, 4]]

tableClear

tableClear(table)

Removes all rows. The columns stay.

tableSet("t", [[1], [2]], ["v"]);
tableClear(@.t);
return [tableGetRows(@.t).length, tableGetColumns(@.t).length];

Output: [0, 2]

Scripts in table blocks

The Filter, Column Mapping, Edit Rows, and Remove Rows blocks run a script once for each row of the input table. In that script:

The value of the last statement (or of a return statement) is the result for that row.

If the script contains an error, the block's print property shows it.

Filter

In the Filter block, the condition script decides which rows to keep. A row is kept when the result is true.

v1 > 10 && $thisRow["Energy Usage"].indexOf("History") > -1

Keep only the first 5 rows that match:

if ($.count == null) $.count = 0;
if (v1 > 10 && $.count < 5) {
  $.count++;
  return true;
}
return false;

Column Mapping

In the Column Mapping block, each output column has a name and a from value.

Columns are calculated in order. A later column can use an earlier result with $rowResult["name"].

name from
total +v1 * v2
label ="Total: " + numberFormat($rowResult["total"], "#,##0.00")
big &$rowResult["total"] > 100
status =$thisRow["Energy Usage"].indexOf("History") > -1

Edit Rows

The Edit Rows block changes cells of an editable table, such as the table property of a Table block. When the block is invoked, the condition script runs for each row. In each row where the result is true, each column n is set to value n. The values are fixed values, not scripts.

status == "open" && priority > 3

Remove Rows

The Remove Rows block removes rows from an editable table. When the block is invoked, the condition script runs for each row, and rows where the result is true are removed.

$thisRow["Device Name"] == "" || value == null