display.grid.applyMap()

display.grid.applyMap()

Description

display.grid.applyMap() runs a function on every record in the grid and replaces the grid contents with the objects the function returns. It is the simplest way to change one or more fields in every record at once.

It works like display.grid.map(), except that map() returns the new records as an array and leaves the grid unchanged, while applyMap() changes the grid itself.

Syntax

display.gridName.applyMap(callback)

Parameters

  1. Callback (Function) - The function to run for each record. It receives the following arguments:

    1. Record (Object) - A copy of the current grid record, with one property for each field in the grid. Changing this object does not change the record in the grid.

    2. Index (Number) - The zero-based index of the record being processed.

    3. Records (Array) - The array of all grid records that is being processed.

    The function must return a record object. The returned object replaces the record in the grid, so return the (modified) record that was passed in, or a new object with the same properties.

     

Return Value

Nothing is returned. The grid is updated in place.

Examples

Example 1: Select or Clear Every Check Box in the Grid

A grid has a check box column bound to an indicator field named checked, and the screen has Select All and Clear All buttons. Setting the property to true or false turns the indicator on or off for every record.

Prerequisite: a Rich Display File named display.json with a grid whose record format name is mygrid, containing a check box bound to an indicator field named checked, and two buttons whose response properties are bound to indicator fields named btnSelectAll and btnClearAll.

pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid // ... load the grid records here ... display.screen.execute(); if (btnSelectAll) { // Turn the check box on in every record display.mygrid.applyMap(record => { record.checked = true; return record; }); } if (btnClearAll) { // Turn the check box off in every record display.mygrid.applyMap(record => { record.checked = false; return record; }); } display.screen.execute(); // Redisplay the grid with the updated check boxes

Example 2: Recalculate a Column for Every Record

Increases the quantity of every record by 10 and writes the result back to the grid.

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.

pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid // Load three records into the grid so there is something to work with display.mygrid.replaceRecords([ { product: 1, description: "ITEM ONE", quantity: 15 }, { product: 2, description: "ITEM TWO", quantity: 20 }, { product: 3, description: "ITEM THREE", quantity: 12 } ]); // Add 10 to the quantity of every record and store the result in the grid display.mygrid.applyMap(function(record) { record.quantity += 10; return record; }); console.log(display.mygrid.getRecords()); // The line above logs: // [ { product: 1, description: 'ITEM ONE', quantity: 25 }, // { product: 2, description: 'ITEM TWO', quantity: 30 }, // { product: 3, description: 'ITEM THREE', quantity: 22 } ]

Related Pages