hub.eb?material_id=195&track_id=264

Iterating over Table Rows


The JavaScript API contains functions to iterate, or loop over, all the rows in a Table.

Using the JavaScript API we can get a Row Iterator for any Table in the Form, for example, var rowIterator = table.myTable.getRows();. Calling next() on the row iterator updates the Table's Current Row to point to the next row in the Table. Any interaction with a Table's Columns, for example tables.myTable.myColumn.value;, addresses that row in the Table.

For example:

// Get the Row Iterator for myTable
var rows = tables.myTable.getRows();

// Iterate over the all the Rows in myTable.
while (rows.next()) {
  // Set myColumn's value to 3.
  tables.myTable.myColumn.value = 3;
}

When the Row Iterator is instructed to move on from the last row in the Table, the next() function returns false and the Table's Current Row is set back to the value is was at prior to the Row Iterator iterating over the Table.

Steps


1

Create a new Form and add a new Table called myTable with integer columns A, B and C.

Populate it in a Before Form Script called myTableScript using the following code:

tables.myTable.insertRow();
tables.myTable.A.value=6;
tables.myTable.B.value=7;

tables.myTable.insertRow();
tables.myTable.A.value=21;
tables.myTable.B.value=2;

tables.myTable.insertRow();
tables.myTable.A.value=14;
tables.myTable.B.value=3;
2

Add the following code to myTableScript:

var rows = tables.myTable.rows;
while (rows.next()) {
  tables.myTable.C.value = tables.myTable.A.value * tables.myTable.B.value;
}
3

Drag the Table onto the Page to create a Table Control.

4

Run the Form, check all three rows and columns have values.

Current Module

Part of tracks

Related