AG Grid: How to Exclude Some Rows in Data Export

In this tutorial, I’m going to show you several approaches for excluding data rows in AG Grid while exporting records. In other words this can be useful when you need to export programmatically selected rows.

Method 1

In AG Grid, you can use the shouldRowBeSkipped callback function to skip rows when exporting the grid data. This function is called for each row in the grid, and should return true if the row should be skipped, or false if it should not be skipped.

The shouldRowBeSkipped function takes a single parameter, which is an object that contains information about the row being processed. This object has the following properties:

  • node: The row node for the row being processed.
  • api: The grid API.
  • context: The grid context.

Here is an example of how you can use the shouldRowBeSkipped function to skip rows that have a certain data property:

function shouldRowBeSkipped(params) {
  // Check if the row's "status" property is "Incomplete"
  return params.node.data.status === 'Incomplete';
}

// Call the "exportDataAsExcel" method on the grid API, passing in the "shouldRowBeSkipped" function
gridApi.exportDataAsExcel({
  shouldRowBeSkipped: shouldRowBeSkipped
});

In this example, the shouldRowBeSkipped function checks whether the row’s “status” property is “Incomplete”, and returns true if it is. This will cause the row to be skipped when exporting or printing the grid. If you want to skip rows based on a different data property or a more complex condition, you can modify the shouldRowBeSkipped function accordingly.

Note that the shouldRowBeSkipped function is not available in all export formats.

Method 2

If you need to skip rows when exporting to a format that does not support the shouldRowBeSkipped function, you can filter the data before exporting it. You can use the gridApi.forEachNode() method to loop through all the nodes in the grid, and exclude the nodes that correspond to the rows you want to skip.

For example:

const dataToExport = [];
gridApi.forEachNode(node => {
  // Check if the row should be skipped based on some condition
  if (node.data.someProperty !== 'someValue') {
    dataToExport.push(node.data);
  }
});

// Call the "exportDataAsExcel" method on the grid API, passing in the filtered data
gridApi.exportDataAsExcel({
  data: dataToExport
});

In this example, the forEachNode method is used to loop through all the nodes in the grid, and exclude the nodes that correspond to rows with someProperty equal to 'someValue'. The remaining rows are added to the dataToExport array, which is then passed to the exportDataAsExcel method to export the data.

Method 3

Similar to above example in this method also, we’re going to filter the data based on some condition.

const selectedData = [];

// Loop through all the nodes in the grid
gridOptionsForDistribution.api.forEachNode(node => {
  // Check if the node has the desired row class
  if (node.data.isHighlighted) {
    node.setSelected(true,false,true);
  }
});

// Call the "exportDataAsExcel" method on the grid API, 
gridApi.exportDataAsExcel({
  onlySelected: true,
});

Then the only the selected rows will be counted using onlySelected option.