display.grid.clear()
Description
display.grid.clear() removes all records from a grid, along with any field error messages and subfile messages that were attached to them. After the call, display.grid.getRecordCount() returns 0 and the next display.grid.write() adds record 1.
The method is typically called before a grid is reloaded, so that records from the previous load are not left behind. It is the equivalent of the RPG SFLCLR keyword being activated before the subfile is rebuilt.
Syntax
display.gridName.clear()Parameters
None.
Return Value
Nothing is returned.
Examples
Example 1: Clear and Reload a Subfile Grid
Clears the grid each time the user presses a Refresh button, then rebuilds it with write(). Without the clear(), each refresh would append a second copy of the records.
Prerequisite: a Rich Display File named subfiled.json with a control record format named ctl and a grid whose record format name is sfl, with the fields product, description, and quantity bound to its columns. The screen also has a button whose response property is bound to an indicator field named btnRefresh.
pjs.defineDisplay("display", "subfiled.json");
function loadGrid() {
display.sfl.clear(); // Start from an empty grid
for (var i = 1; i <= 25; i++) {
product = i; // Set the global fields for the record...
description = "ITEM " + i;
quantity = i * 2;
display.sfl.write(); // ...and write it to the grid
}
}
loadGrid();
display.ctl.execute(); // Display the control format and the grid
while (btnRefresh) { // Rebuild the grid each time Refresh is pressed
loadGrid();
display.ctl.execute();
}Example 2: Clear a Grid That Was Loaded from an Array
Shows that clear() empties a grid regardless of how it was loaded.
Prerequisite: a Rich Display File named display.json with a record format named screen that contains a grid whose record format name is mygrid, with the fields product and description bound to its columns.
pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid
display.mygrid.addRecords([
{ product: 1, description: "ITEM ONE" },
{ product: 2, description: "ITEM TWO" }
]);
console.log(display.mygrid.getRecordCount()); // 2
display.mygrid.clear();
console.log(display.mygrid.getRecordCount()); // 0