When working with large datasets in enterprise applications, handling user interactions efficiently becomes critical. One common requirement is detecting when a user selects a row or cell within a data grid. The selectionChanged event in AG Grid provides a powerful mechanism to capture these interactions, enabling developers to build responsive and dynamic user interfaces.
Understanding the selectionChanged Event
The selectionChanged event fires whenever the user changes their selection within the grid. This includes selecting a new row, deselecting an existing selection, or switching between multiple selected items. Unlike simple click events, selectionChanged specifically tracks the state of the grid's selection model, making it ideal for scenarios where you need to react to user choices in real-time.
Basic Implementation
To get started, you'll need to set up your grid with the onSelectionChanged callback. This function will be triggered every time the selection state changes. Here's a foundational example:

const gridOptions = {
columnDefs: [
{ field: 'name' },
{ field: 'age' },
{ field: 'country' }
],
rowSelection: 'multiple',
onSelectionChanged: onSelectionChanged
};
function onSelectionChanged(event) {
const selectedRows = event.api.getSelectedRows();
console.log('Selected rows:', selectedRows);
}
This basic setup allows you to capture all selected rows whenever the user makes a selection change. The getSelectedRows() method returns an array of data objects corresponding to the currently selected rows.
Advanced Selection Handling
For more complex scenarios, you might want to differentiate between single and multiple selections, or handle cell selection separately. AG Grid provides granular control through different event properties and methods.
Row vs Cell Selection
When working with cell selection, you can use getSelectedCells() instead of getSelectedRows(). This is particularly useful for spreadsheet-like applications where users need to copy, edit, or format individual cells.

function onSelectionChanged(event) {
if (event.type === 'cell') {
const selectedCells = event.api.getSelectedCells();
// Handle cell selection logic
} else {
const selectedRows = event.api.getSelectedRows();
// Handle row selection logic
}
}
Practical Use Cases
The selectionChanged event shines in real-world applications. Consider a dashboard where selecting a row should update a detail panel, or a data entry form where cell selection triggers validation. The event's flexibility allows you to build sophisticated interactions without complex state management.
Performance Considerations
When dealing with large datasets, be mindful of performance. Frequent selection changes can trigger multiple selectionChanged events. Implement debouncing or batching if your handler performs expensive operations like API calls or complex DOM updates.
let selectionTimeout;
function onSelectionChanged(event) {
clearTimeout(selectionTimeout);
selectionTimeout = setTimeout(() => {
const selectedRows = event.api.getSelectedRows();
updateDetailPanel(selectedRows);
}, 100);
}
Common Pitfalls
Developers often make the mistake of assuming selectionChanged only fires on row selection. Remember it also fires on cell selection and deselection. Always check the event type and handle both scenarios appropriately. Another common issue is not cleaning up event listeners when the grid is destroyed, which can lead to memory leaks.
Integration with Frameworks
Whether you're using React, Angular, or Vue, the core concept remains the same. The framework-specific wrappers provide the same selectionChanged callback, ensuring consistent behavior across different tech stacks.
By mastering the selectionChanged event, you can create more interactive and responsive grid applications that provide immediate feedback to user actions, enhancing the overall user experience in data-intensive applications.