display.grid.findIndex()
Description
display.grid.findIndex() returns the zero-based index of the first record in the grid that passes the test implemented by the provided function. Records are tested in grid order and testing stops at the first match. If no record passes the test, -1 is returned.
It is modeled on the JavaScript Array.prototype.findIndex() method. The index it returns can be passed to methods such as display.grid.delete(), display.grid.insert(), and display.grid.splice(). To get the record itself, use display.grid.find().
Syntax
display.gridName.findIndex(callback)Parameters
Callback (Function) - The function to run for each record. It receives the following arguments:
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.
Index (Number) - The zero-based index of the record being processed.
Records (Array) - The array of all grid records that is being processed.
The function must return
truewhen the record is the one being searched for.
Return Value
Number - The zero-based index of the first matching record, or -1 if no record matches.
Examples
Example 1: Find the Position of a Record
Returns the index of the first record with a quantity below 15. Record 3 is the first such record, and its zero-based index is 2.
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 four 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 },
{ product: 4, description: "ITEM FOUR", quantity: 18 }
]);
// Find the index of the first record with a quantity less than 15
var index = display.mygrid.findIndex(function(record) {
return record.quantity < 15;
});
console.log(index); // 2Example 2: Delete a Record Located by Value
Uses the index returned by findIndex() to delete the record for product 2. The index is checked for -1 first so that nothing is deleted when the product is not in 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 four 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 },
{ product: 4, description: "ITEM FOUR", quantity: 18 }
]);
var index = display.mygrid.findIndex(record => record.product === 2);
if (index !== -1) {
display.mygrid.delete(index); // Remove the matching record
}
console.log(display.mygrid.getRecordCount()); // 3