display.grid.delete()
Description
display.grid.delete() removes one record from a grid. The record can be identified by its zero-based index, or, when the index is omitted, by the value of the grid's relative record number (RRN) field.
The RRN field is the field named in the rrnFields configuration option of pjs.defineDisplay() or set with display.grid.setGridRecNumField(). It is set automatically by display.grid.getRecord() and display.grid.readChanged(), so a record that was just read can be deleted without passing an index.
Syntax
display.gridName.delete(index) // delete by zero-based index
display.gridName.delete() // delete the record identified by the RRN fieldParameters
Index (Number, optional) - The zero-based index of the record to remove; the first record is index
0. If omitted, the record identified by the grid's RRN field is removed. Unlike an index, a relative record number is 1-based, so RRN1is the first record.
Return Value
Nothing is returned.
If the index is omitted and no RRN field is associated with the grid, the last record in the grid is removed. Always pass an index, or define an RRN field, so that the intended record is deleted.
Examples
Example 1: Delete a Record by Index
Removes the first record (index 0) from a grid of three records.
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" },
{ product: 3, description: "ITEM THREE" }
]);
display.mygrid.delete(0); // Remove the first record
console.log(display.mygrid.getRecords());
// The line above logs:
// [ { product: 2, description: 'ITEM TWO' },
// { product: 3, description: 'ITEM THREE' } ]Example 2: Delete the Records the User Changed
Reads each record the user changed with readChanged() and deletes it. Because readChanged() sets the RRN field, no index is needed. Deleting shifts the remaining records down, so the loop reads the changed records into an array first and deletes them from the highest RRN to the lowest.
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" }, and the grid has a check box column so that checking a row marks the record as changed.
pjs.defineDisplay("display", "subfiled.json", { rrnFields: { sfl: "rrn" } });
pjs.define("rrn", { type: "integer" }); // RRN field named in rrnFields
// ... load the grid records with display.sfl.write() here ...
display.ctl.execute(); // Let the user check the rows to delete
// Collect the RRN of every changed record
var toDelete = [];
display.sfl.readChanged();
while (!display.endOfData()) {
toDelete.push(rrn); // readChanged() placed the record's RRN in the rrn field
display.sfl.readChanged();
}
// Delete from the bottom up so earlier RRNs are still valid after each delete
toDelete.reverse().forEach(function(recordNumber) {
rrn = recordNumber;
display.sfl.delete(); // No index: the record identified by rrn is removed
});
display.ctl.execute();