display.grid.getCopyOfData()

display.grid.getCopyOfData()

Description

display.grid.getCopyOfData() returns a copy of the grid's internal data as a JavaScript array. Each element of the array is itself an array holding the raw field values of one record, in the order returned by display.grid.getFieldNames(). Because the result is a copy, changing it does not change the grid.

For most purposes display.grid.getRecords() is more convenient, because it returns each record as an object keyed by field name and converts numeric fields to numbers. getCopyOfData() is useful when the raw values are needed, for example to take a snapshot of the grid that can be compared later, or to export the data in a positional format.

Syntax

display.gridName.getCopyOfData()

Parameters

None.

Return Value

Array - A copy of the grid data. Each element is an array of the record's field values, stored as strings, in the same order as the field names returned by getFieldNames().

Examples

Example 1: Take a Snapshot of the Grid

Copies the raw grid data, then shows that changing the copy does not affect the grid.

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" } ]); var snapshot = display.mygrid.getCopyOfData(); console.log(snapshot); // The line above logs the raw values, one array per record: // [ [ '1', 'ITEM ONE' ], // [ '2', 'ITEM TWO' ] ] snapshot[0][1] = "CHANGED"; // Change the copy... console.log(display.mygrid.getRecords()[0]); // ...the grid is unchanged: // { product: 1, description: 'ITEM ONE' }

Example 2: Build a CSV Export from the Raw Data

Combines getFieldNames() and getCopyOfData() to build comma-separated text with a header line, which could be written to a file or returned from a web service.

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" } ]); var lines = []; lines.push(display.mygrid.getFieldNames().join(",")); // Header line display.mygrid.getCopyOfData().forEach(function(row) { lines.push(row.join(",")); // One line per record }); console.log(lines.join("\n"));

Related Pages