display.grid.readChanged()
Description
display.grid.readChanged() retrieves the next record in the grid that was changed by the user since the screen was last displayed. The record data is placed into the program's global fields, or into a data structure if one is passed, and is also returned as an object. It is the equivalent of the RPG READC operation.
Each call returns the next changed record after the one returned by the previous call. When no more changed records remain, display.endOfData() returns true, an empty object is returned, and the read position is reset to the start of the grid. The record returned by readChanged() becomes the current record, so it can be updated with display.grid.update() or deleted with display.grid.delete().
A record is marked as changed when the user modifies an input field in the row, or, for a row that has no input fields, when the grid's subfile next changed property was set when the record was written.
Syntax
display.gridName.readChanged()
display.gridName.readChanged(dataStructure)Parameters
Data structure (Object, optional) - A Profound.js data structure reference. If specified and a record is found, the referenced data structure is populated with the record data instead of the global fields. If the data structure is not qualified, wrap the parameter with the pjs.ds() API.
Return Value
Object - If a changed record is found, an object containing the record's fields as properties. The values are the raw stored values, so numeric fields are returned as strings. If no changed record remains, an empty object {} is returned and display.endOfData() returns true.
Examples
Example 1: Process Every Record the User Changed
Displays the grid, then loops through the changed records with the same structure as an RPG READC loop, ending when display.endOfData() is true.
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");
// ... load the grid records with display.sfl.write() here ...
display.ctl.execute(); // Display the control format and grid; the user edits some rows
// Process all grid records changed by the user
display.sfl.readChanged();
while (!display.endOfData()) {
// The global fields now hold the changed record's values
console.log("Product " + product + " quantity changed to " + quantity);
display.sfl.readChanged(); // Get the next changed record
}Example 2: Validate Changed Records and Update Them
Reads each changed record, corrects a value if needed, and writes the correction back to the same record with update(). The RRN field is set by readChanged(), so the program can also report which record was changed.
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 display file is defined with rrnFields: { sfl: "rrn" }.
pjs.defineDisplay("display", "subfiled.json", { rrnFields: { sfl: "rrn" } });
pjs.define("rrn", { type: "integer" });
// ... load the grid records here ...
display.ctl.execute();
display.sfl.readChanged();
while (!display.endOfData()) {
if (quantity < 0) {
quantity = 0; // Negative quantities are not allowed
display.sfl.update(); // Save the corrected value in record number rrn
console.log("Corrected record " + rrn);
}
display.sfl.readChanged();
}
display.ctl.execute();Example 3: Read Changed Records into a Data Structure
Uses the returned object instead of the global fields, which is convenient when the record only needs to be passed to another function.
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");
// ... load the grid records here ...
display.ctl.execute();
var record = display.sfl.readChanged();
while (!display.endOfData()) {
saveProduct(record); // record has product, description, and quantity properties
record = display.sfl.readChanged();
}
function saveProduct(record) {
// Write the changed record to the database here
}RPG Equivalent
READC