display.grid.splice()
Description
display.grid.splice() changes the contents of a grid by removing existing records and/or inserting new records at a given zero-based index. It is modeled on the JavaScript Array.prototype.splice() method and combines the behavior of display.grid.delete() and display.grid.insert() in a single call, which is useful for replacing records.
Syntax
display.gridName.splice(start, deleteCount)
display.gridName.splice(start, deleteCount, record1, record2, ...)Parameters
Start (Number) - The zero-based index at which to start changing the grid. If greater than the number of records in the grid, no records are removed and any new records are added at the end. If negative, the position is counted from the end of the grid, so
-1is the last record.Delete count (Number) - The number of records to remove, starting at the Start index. Use
0to insert records without removing any. If the count is greater than the number of records remaining from the start index, all records through the end of the grid are removed.Record (Object, optional) - A record object to insert at the Start index, with properties representing the fields in the grid.
Additional records (Object, optional) - One or more additional record objects to insert after the first one.
The Delete count parameter must always be passed. Unlike the JavaScript array method, omitting it does not remove the records through the end of the grid; instead, no records are removed.
Return Value
Nothing is returned. The grid is updated in place.
Examples
Example 1: Replace Two Records with One
Starting at index 1, removes two records (products 2 and 3) and inserts one record in their place.
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: 1, description: "ITEM ONE" },
{ product: 2, description: "ITEM TWO" },
{ product: 3, description: "ITEM THREE" },
{ product: 4, description: "ITEM FOUR" }
]);
// At index 1, remove 2 records and insert 1 record
display.mygrid.splice(1, 2, { product: 99, description: "NINETY NINE" });
console.log(display.mygrid.getRecords());
// The line above logs:
// [ { product: 1, description: 'ITEM ONE' },
// { product: 99, description: 'NINETY NINE' },
// { product: 4, description: 'ITEM FOUR' } ]Example 2: Remove a Range of Records
Removes three records starting at index 1 without inserting anything.
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: 1, description: "ITEM ONE" },
{ product: 2, description: "ITEM TWO" },
{ product: 3, description: "ITEM THREE" },
{ product: 4, description: "ITEM FOUR" },
{ product: 5, description: "ITEM FIVE" }
]);
display.mygrid.splice(1, 3); // Remove records at indexes 1, 2, and 3
console.log(display.mygrid.getRecords());
// The line above logs:
// [ { product: 1, description: 'ITEM ONE' },
// { product: 5, description: 'ITEM FIVE' } ]Example 3: Replace a Record Located by Value
Uses display.grid.findIndex() to find the record for product 3 and replaces it with an updated record. A delete count of 1 with one new record swaps the record in place.
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 index = display.mygrid.findIndex(record => record.product === 3);
if (index !== -1) {
// Replace the record at that index with a new version
display.mygrid.splice(index, 1, { product: 3, description: "ITEM THREE (REVISED)", quantity: 30 });
}
console.log(display.mygrid.getRecords()[2]);
// { product: 3, description: 'ITEM THREE (REVISED)', quantity: 30 }