display.grid.filter()
Description
display.grid.filter() returns an array of all the records in the grid that pass the test implemented by the provided function. The grid itself is not changed; to remove the non-matching records from the grid, use display.grid.applyFilter() instead.
It is modeled on the JavaScript Array.prototype.filter() method.
Syntax
display.gridName.filter(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
truefor each record that should be included in the result.
Return Value
Array - The records for which the callback returned a truthy value, in grid order. Each record object is a copy of the grid record. Numeric fields (packed, zoned, and float) are returned as JavaScript numbers, character fields as strings, and indicator fields as the strings "1" and "0". Changing the object does not change the record in the grid. An empty array is returned when no record matches.
Examples
Example 1: Find All Records Over a Quantity
Returns the two records with a quantity greater than 15. The grid still holds all four records afterwards.
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 }
]);
// Get the records with a quantity greater than 15
var records = display.mygrid.filter(function(record) {
return record.quantity > 15;
});
console.log(records);
// The line above logs:
// [ { product: 2, description: 'ITEM TWO', quantity: 20 },
// { product: 4, description: 'ITEM FOUR', quantity: 18 } ]
console.log(display.mygrid.getRecordCount()); // 4 - the grid itself is unchangedExample 2: Process the Rows the User Selected
A grid has a check box column bound to an indicator field named selected. After the user presses a button, the selected records are collected and processed. Indicator values are held in record objects as the strings "1" and "0".
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 selected and a product field.
pjs.defineDisplay("display.json"); // Object name defaults to "display"; display.json defines mygrid
// ... load the grid records here ...
display.screen.execute(); // Let the user check some rows
// Collect the records whose check box is on
var selected = display.mygrid.filter(record => record.selected === "1");
selected.forEach(function(record) {
console.log("Selected product: " + record.product);
// Process the selected product here
});