Caost Ideas

"Ag-Grid: SelectionChanged Event Example - Mastering Row Selection"

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:

AG Grid Blog - JavaScript Data Grid supporting React, Angular, and Vue

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.

AG Grid Figma Design System Updates - v30.2.0

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.

AG Grid Blog - JavaScript Data Grid supporting React, Angular, and Vue

AG Grid Blog - JavaScript Data Grid supporting React, Angular, and Vue

AG Grid Figma Design System Updates - v30.2.0

AG Grid Figma Design System Updates - v30.2.0

AG Grid: Column with checkbox (boolean value)? - Dash Python - Plotly ...

AG Grid: Column with checkbox (boolean value)? - Dash Python - Plotly ...

How to use ag grid in angular | From Setup to Advance - YouTube

How to use ag grid in angular | From Setup to Advance - YouTube

Ag-grid how to format rows/row groups? - streamlit-aggrid - Streamlit

Ag-grid how to format rows/row groups? - streamlit-aggrid - Streamlit

AG Grid — a data grid or pivot table library

AG Grid — a data grid or pivot table library

Ag Grid Nested Table Example at Georgia Jarman blog

Ag Grid Nested Table Example at Georgia Jarman blog

Angular Grid: AG Grid Design System | AG Grid

Angular Grid: AG Grid Design System | AG Grid

Demo - HR Management

Demo - HR Management

Ag Grid Reset Button at Cole Sherrell blog

Ag Grid Reset Button at Cole Sherrell blog

ScheduleJS into AG-Grid

ScheduleJS into AG-Grid

Using AG Grid with TypeScript and JavaScript

Using AG Grid with TypeScript and JavaScript

VueJS Examples, How To and Tutorials for AG Grid

VueJS Examples, How To and Tutorials for AG Grid

Ag Grid Reset Sort at Steve Courtney blog

Ag Grid Reset Sort at Steve Courtney blog

AG Grid Cell Rendering Pipeline with TypeScript

AG Grid Cell Rendering Pipeline with TypeScript

Using AG Grid in React: Guide and alternatives - LogRocket Blog

Using AG Grid in React: Guide and alternatives - LogRocket Blog

React Data Grid and React Table Examples with AG Grid

React Data Grid and React Table Examples with AG Grid

Ag Grid Icons List

Ag Grid Icons List

Ag Grid Reset Filter at Joseph Cornwall blog

Ag Grid Reset Filter at Joseph Cornwall blog

The Dash AG Grid: A Guide For Creating Rich Data Tables

The Dash AG Grid: A Guide For Creating Rich Data Tables

Read Next