display.grid.shift()

display.grid.shift()

Description

display.grid.shift() removes the first record from the grid and returns it as a JavaScript object. The remaining records move up, so the second record becomes the first. It is modeled on the JavaScript Array.prototype.shift() method. To remove the last record instead, use display.grid.pop().

Syntax

display.gridName.shift()

Parameters

None.

Return Value

Object - The removed record, with one property for each field in the grid. Numeric fields are returned as numbers. If the grid is empty, undefined is returned and nothing is removed.

Examples

Example 1: Remove the First Record

Removes the first record from a grid of three and shows the remaining 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" } ]); var removed = display.mygrid.shift(); console.log(removed); // { product: 1, description: 'ITEM ONE' } console.log(display.mygrid.getRecords()); // The line above logs: // [ { product: 2, description: 'ITEM TWO' }, // { product: 3, description: 'ITEM THREE' } ]

Example 2: Process a Grid as a Queue

Treats the grid as a work queue: each pass takes the first record, processes it, and stops when the grid is empty. Checking for undefined ends the loop safely.

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" } ]); var record = display.mygrid.shift(); while (record !== undefined) { console.log("Processing product " + record.product); record = display.mygrid.shift(); // Take the next record from the front of the grid } console.log(display.mygrid.getRecordCount()); // 0

Related Pages