display.grid.replaceRecords()

display.grid.replaceRecords()

Description

display.grid.replaceRecords() clears a grid and then adds an array of records to it, so that the grid contains exactly the records in the array. It is equivalent to calling display.grid.clear() followed by display.grid.addRecords().

This is the usual way to reload a grid from a fresh query result, and it is the companion of display.grid.getRecords(): get the records, work with the array, and put the result back with replaceRecords().

Syntax

display.gridName.replaceRecords(records)

Parameters

  1. Records (Array) - An array of record objects. Each object has a property for each field in the grid; a grid field with no matching property is stored as blank. Passing an empty array simply clears the grid.

Return Value

Nothing is returned.

Examples

Example 1: Replace the Contents of a Grid

Loads two records, then replaces them with four different records. Only the four new records remain.

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: 98, description: "OLD ITEM" }, { product: 99, description: "ANOTHER OLD ITEM" } ]); // Replace everything in the grid with these records display.mygrid.replaceRecords([ { product: 1, description: "ITEM ONE" }, { product: 2, description: "ITEM TWO" }, { product: 3, description: "ITEM THREE" }, { product: 4, description: "ITEM FOUR" } ]); console.log(display.mygrid.getRecordCount()); // 4

Example 2: Reload a Grid When the User Changes a Filter

Each time the user enters a search value and presses Enter, the grid is reloaded from a parameterized query with pjs.query(). Because replaceRecords() clears the grid first, the previous results never accumulate.

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, description, and quantity bound to its columns. The example also assumes a table PRODUCTSP with the columns PRID, PRNAME, and PRQTY, an input field on the screen bound to searchText, and an Exit button whose response property is bound to an indicator field named btnExit.

pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid while (!btnExit) { var rows = pjs.query( "SELECT prid AS product, prname AS description, prqty AS quantity FROM productsp WHERE prname LIKE ?", ["%" + searchText.trim() + "%"] ); display.mygrid.replaceRecords(rows); // Show only the matching products; previous results are discarded display.screen.execute(); // Wait for a new search value or the Exit button }

Video Tutorial


Related Pages