display.grid.getRecords()
Description
display.grid.getRecords() extracts all records from a grid and returns them as an array. Each element of the array is a JavaScript object with one property for each field in the grid.
The array is a copy of the records: modifying it does not modify the grid. A common pattern is to get the records, change the array, and then place it back into the grid with display.grid.replaceRecords(). To return only part of the grid, use display.grid.slice().
Syntax
display.gridName.getRecords()Parameters
None.
Return Value
Array - The grid records in order. Each record object is a copy of the grid record. Numeric fields (packed, zoned, and float) are returned as JavaScript numbers, character fields as strings, and indicator fields as the strings "1" and "0". Changing the object does not change the record in the grid. An empty array is returned when the grid is empty.
Examples
Example 1: Get the Records Written with write()
Writes two records using the global fields and then retrieves them as objects. Note that the numeric product field is returned as a number.
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
product = 1;
description = "ITEM ONE";
display.mygrid.write();
product = 2;
description = "ITEM TWO";
display.mygrid.write();
console.log(display.mygrid.getRecords());
// The line above logs:
// [ { product: 1, description: 'ITEM ONE' },
// { product: 2, description: 'ITEM TWO' } ]Example 2: Modify the Records and Put Them Back
Gets the records, changes the array, and replaces the grid contents with the result. This is useful when a change is easier to express on the whole array than with the individual grid methods.
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, description, and quantity bound to its columns.
pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid
// Load three records into the grid so there is something to work with
display.mygrid.replaceRecords([
{ product: 1, description: "ITEM ONE", quantity: 15 },
{ product: 2, description: "ITEM TWO", quantity: 20 },
{ product: 3, description: "ITEM THREE", quantity: 12 }
]);
var records = display.mygrid.getRecords(); // A copy of the records
// Work with the array using any JavaScript array method
records.forEach(record => record.description = record.description.toLowerCase());
records.push({ product: 4, description: "item four", quantity: 18 });
display.mygrid.replaceRecords(records); // Put the changed array back into the grid
console.log(display.mygrid.getRecordCount()); // 4Example 3: Return Grid Data from a Web Service
Returns the grid contents as JSON from a Profound.js web service module. Because the records are plain objects, they can be passed straight to response.send().
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, description, and quantity bound to its columns. The module is called as a web service, so request and response are available.
function getProducts(request, response) {
pjs.defineDisplay("display.json");
// ... load the grid records here ...
response.send({ products: display.mygrid.getRecords() });
}
exports.default = getProducts;