Programmatically selecting and deselecting row in ag-grid

2 min read 06-10-2024
Programmatically selecting and deselecting row in ag-grid


Mastering Row Selection in ag-Grid: A Developer's Guide

Ag-Grid, a powerful and versatile data grid component, offers robust row selection capabilities. While the built-in features are user-friendly, developers often require programmatic control over row selection, enabling dynamic interaction and data manipulation. This article will guide you through the process of programmatically selecting and deselecting rows in Ag-Grid, empowering you to build interactive and dynamic data grids.

Scenario and Code Example

Imagine a scenario where you need to select a specific row based on a user action, like clicking a button or filtering data. Let's assume you have a simple grid with an "Employee" column.

HTML:

<div id="myGrid" style="width: 500px; height: 200px;"></div>
<button id="selectRowButton">Select Row 2</button>

JavaScript:

const gridOptions = {
  columnDefs: [
    { field: 'employee', sortable: true, filter: true },
  ],
  rowData: [
    { employee: 'John Doe' },
    { employee: 'Jane Smith' },
    { employee: 'Peter Jones' },
  ],
};

const gridDiv = document.querySelector('#myGrid');
const gridApi = new agGrid.Grid(gridDiv, gridOptions);

document.getElementById('selectRowButton').addEventListener('click', () => {
  // Code for programmatically selecting row 2 goes here
});

Programmatic Row Selection

Ag-Grid offers the api.getDisplayedRowAtIndex() function to access a specific row based on its index in the displayed dataset. We can then use the api.selectIndex() function to programmatically select the desired row.

Complete JavaScript with row selection:

// ... (Previous code)

document.getElementById('selectRowButton').addEventListener('click', () => {
  const rowNode = gridApi.getDisplayedRowAtIndex(1); // Row 2 has index 1
  gridApi.selectIndex(rowNode.rowIndex); 
});

Deselecting Rows

To deselect rows, we can leverage the api.deselectIndex() function. Let's modify the example to deselect the current selection before selecting the new row.

JavaScript with deselection:

// ... (Previous code)

document.getElementById('selectRowButton').addEventListener('click', () => {
  gridApi.deselectAll(); // Deselect any existing selections
  const rowNode = gridApi.getDisplayedRowAtIndex(1);
  gridApi.selectIndex(rowNode.rowIndex);
});

Extending Functionality: Filtering and Selection

We can extend this principle to handle more complex scenarios. For example, imagine selecting the first row matching a specific filter criteria.

JavaScript with filtering and selection:

document.getElementById('selectRowButton').addEventListener('click', () => {
  // Apply filter to find employee "Jane Smith"
  gridApi.setQuickFilter('Jane Smith'); 
  
  // Find the first matching row and select it
  const rowNode = gridApi.getDisplayedRowAtIndex(0);
  gridApi.selectIndex(rowNode.rowIndex); 
});

Important Considerations

  • Row Index: Remember that getDisplayedRowAtIndex() retrieves the index based on the displayed dataset, which might differ from the original data index.
  • Multiple Selections: Use api.selectIndex(rowNode.rowIndex, true) to allow multiple row selections.
  • Synchronization: Ensure your programmatic selections are synchronized with user interactions. For example, if a user deselects a row, your code should reflect this change.

Wrapping Up

By leveraging Ag-Grid's API, you can effectively programmatically select and deselect rows, enhancing the interactivity and functionality of your data grid. Remember to adapt the code snippets to match your specific data structure and interaction logic. Experiment with the examples provided to gain a deeper understanding of row selection and unlock the full potential of Ag-Grid's features.