display.grid.write()

display.grid.write()

Description

display.grid.write() writes a record to a grid using the current values of the program's global fields, or of a data structure if one is passed. It is the equivalent of the RPG WRITE operation against a subfile record format, and it is the method most often used to load a grid in applications converted from RPG.

If the grid has a relative record number (RRN) field, assigned with the rrnFields option of pjs.defineDisplay() or with display.grid.setGridRecNumField(), the record is written at the record number held in that field, replacing any record already there. If there is no RRN field, the record is added to the end of the grid.

To load a grid from an array of objects instead of from fields, use display.grid.addRecords() or display.grid.replaceRecords().

Syntax

display.gridName.write() display.gridName.write(dataStructure)

Parameters

  1. Data structure (Object, optional) - A Profound.js data structure reference. If specified, the record is populated with the values from the data structure. If not specified, the global field values are used. The parameter is required when the display file was defined with defineFields: false.

Return Value

Nothing is returned.

Examples

Example 1: Load a Grid with a Loop

Clears the grid and writes 99 records to it, setting the global fields before each write(). Because no RRN field is defined, each record is added to the end.

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.

pjs.defineDisplay("display", "subfiled.json"); display.sfl.clear(); for (var i = 1; i <= 99; i++) { product = i; // Populate the grid fields for this record description = "ITEM " + i; quantity = i * 10; display.sfl.write(); // Write the record to the end of the grid } display.screen.execute();

Example 2: Load a Grid from a Database File

Reads a file with the record-level access APIs and writes a grid record for each row, which mirrors a typical RPG subfile load loop.

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 example also assumes a database file PRODUCTSP whose fields PRID, PRNAME, and PRQTY are defined with pjs.defineTable().

pjs.defineDisplay("display", "subfiled.json"); pjs.defineTable("productsp", { read: true }); display.sfl.clear(); productsp.fetchNext(); // Read the first record while (!productsp.endOfData()) { product = prid; // Move file fields to grid fields description = prname; quantity = prqty; display.sfl.write(); // Write a grid record for this row productsp.fetchNext(); // Read the next record } display.ctl.execute();

Example 3: Write to Specific Record Numbers with an RRN Field

Defines an RRN field with the rrnFields option. Each write() stores the record at the number held in rrn, so records can be written, and later overwritten, by position.

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.

pjs.defineDisplay("display", "subfiled.json", { rrnFields: { sfl: "rrn" } }); pjs.define("rrn", { type: "integer" }); display.sfl.clear(); for (rrn = 1; rrn <= 99; rrn++) { product = rrn; description = "ITEM " + rrn; quantity = 0; display.sfl.write(); // Written as record number rrn } // Overwrite record 5 in place rrn = 5; product = 5; description = "ITEM FIVE (REPLACED)"; quantity = 50; display.sfl.write(); display.screen.execute();

RPG Equivalent

WRITE

Video Tutorial


Related Pages