Search code examples
ag-gridag-grid-react

ag-grid: is there a way to trigger event when clear range selection


When range selection happened, I can listen to rangeSelectionChanged event. Is there any event I can listen to when all selected range are cleared?


Solution

  • If I understood correctly by selected range are cleared I am assuming user is randomly clicking on any cell in the grid thus only one cell is getting highligheted and range is cleared. for this there is no separate event so you have to check the CellRange object in that case. below logic you can use inside rangeSelectionChanged event

    onRangeSelectionChanged(event) {
    ...
    ...
    
    //lets check here if selection is cleared by user or not
    var cellRanges = this.gridApi.getCellRanges(); // this gives the cellrange object.
    var startRowIndex = cellRanges[0].startRow.rowIndex;
    var endRowIndex = cellRanges[0].endRow.rowIndex;
    var NumberOfColumns = cellRanges[0].columns.length;
    
    //cellRange.length shows how many different ranges user has selected in Grid
    
    if(startRowIndex === endRowIndex && NumberOfColumns ===1 && cellRanges.length<2)
    {
    //do your processing here 
    }