display.grid.getFieldNames()
Description
display.grid.getFieldNames() returns an array of strings containing the names of the fields bound to the grid, in the order in which they are stored. This is the same order used by the arrays returned from display.grid.getCopyOfData().
The names are returned as they are known to the program. When the display file is running in the default compatibility mode (where format and field names are not case-sensitive), the names are returned in upper case.
Syntax
display.gridName.getFieldNames()Parameters
None.
Return Value
Array - An array of strings, one for each field in the grid.
Examples
Example 1: List the Fields in a Grid
Logs the names of the fields bound 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
var fields = display.mygrid.getFieldNames();
console.log(fields); // [ 'PRODUCT', 'DESCRIPTION', 'QUANTITY' ]Example 2: Check Whether a Grid Has a Field Before Using It
A generic routine that works with several grids checks for a quantity field before totaling it. The comparison ignores case so it works in either naming mode.
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 }
]);
var hasQuantity = display.mygrid.getFieldNames().some(function(name) {
return name.toLowerCase() === "quantity";
});
if (hasQuantity) {
var total = display.mygrid.reduce((sum, record) => sum + record.quantity, 0);
console.log("Total quantity: " + total); // Total quantity: 47
}