Understanding the selectionchanged event is fundamental for developers working with ag-Grid, as it provides a direct mechanism to respond to user interactions. This specific event fires immediately after the selection state of the grid data changes, whether through mouse clicks, keyboard navigation, or programmatic updates. By leveraging this event, you can create dynamic and reactive applications that instantly reflect user choices. The selectionchanged example patterns discussed here will help you build more interactive data dashboards and complex data management interfaces.
Capturing User Selections in Real-Time
The core of interactivity in ag-Grid revolves around listening for changes in the model. The selectionchanged event acts as a bridge between the UI and your application logic. Instead of polling the grid for changes, you register a listener that activates precisely when the user selects or deselects rows. This allows for immediate validation, data synchronization with other components, or triggering side effects based on the current selection state.
Basic Implementation Strategy
To implement a working selectionchanged example, you first need to add the listener to your grid options. You define a method in your component that contains the logic to execute when the event fires. This method receives an event object containing details about the node, the current selection state, and the type of change that occurred. Connecting this listener is the essential first step in building reactive grid interactions.

Practical Code Structure
Looking at a concrete selectionchanged example helps solidify the concept. You typically define the `onSelectionChanged` method within your component's class. Inside this method, you access the grid API via the event params to retrieve the selected rows using `api.getSelectedRows()`. This returns a standard JavaScript array of the data objects currently highlighted, which you can then use to update other parts of your user interface or send to a backend service.
Handling Multi-Selection Scenarios
Modern applications rarely rely on single selection; they require robust handling of multiple selections. The selectionchanged event excels in these scenarios because it captures the delta of change. Whether the user is using Shift-click to select a range or holding Ctrl to pick individual items, the event provides the context needed to manage complex selection logic. Your example code should iterate through the selected rows to process data accurately, ensuring performance remains optimal even with large datasets.
Advanced Integration Techniques
Beyond simply reading the selection, you can use this event to control other UI elements. A common pattern involves enabling or disabling action buttons based on whether a row is selected. For instance, a "Delete" button might remain inactive until the selectionchanged event confirms that at least one row is chosen. This creates a fluid user experience where the interface adapts intelligently to the user's current focus within the grid.

Data Synchronization with Other Components
In a Master-Detail interface, the selectionchanged event is the master conductor. When a user selects a row in the master grid, the event triggers a call to fetch detailed information related to that specific record. This detail view can exist in a separate component or a modal window, updating instantly to reflect the new selection. This pattern is widely used in CRM and enterprise applications to provide deep insights without navigating away from the main data list.
Best Practices and Performance
While implementing your selectionchanged example, it is vital to consider performance. If the logic inside the listener is heavy, it might cause lag during rapid selections. To mitigate this, ensure you are not performing expensive DOM manipulations directly inside the handler without debouncing. Utilize the node provided in the params to check if specific rows are selected rather than processing the entire dataset on every single change, keeping the grid snappy and responsive.























