0

I am using Ag-grid library for grid view in React app. Following is my Ag-Grid component:

const handleChanged = (gridOptions) => {
    const selectedNodes = gridOptions.api.getSelectedNodes()
    //TODO
}
            <AgGridReact
                data-testid="details-data"
                columnDefs={DetailsColDef}
                rowData={formatDetailsData(
                    data?.Response,
                    false
                )}
                rowSelection="single"
                reactNext={true}
                defaultColDef={defaultColDef}
                onSelectionChanged={handleSelected}
                suppressPaginationPanel={true}
                domLayout="autoHeight"
                suppressMaxRenderedRowRestriction={true}
                rowBuffer={5}
                suppressColumnVirtualisation={false}
                debounceVerticalScrollbar={true}
                alwaysShowVerticalScroll={true}
            ></AgGridReact>

Current Scenario: handleChanged is getting called when we click on Grid row.
Requirement: Need to call handleChanged event every time on multiple click at same time. Currently event is getting called first time only. If we click again on same row, it need's to be called.

Akshay phalphale
  • 145
  • 3
  • 14
  • 1
    check [this answer](https://stackoverflow.com/a/71105532/13405106) add `rowMultiSelectWithClick={true}` – Usama Nov 03 '22 at 18:47
  • 1
    @Usama Thank you for your answer. It's working for deselecting the row. But I want data of row on deselecting it. – Akshay phalphale Nov 03 '22 at 19:08

2 Answers2

2

You can call gridOptions.api.getSelectedNodes() inside onCellClicked. Every time the row/cell is clicked you will get the data

const onCellClicked = (e) => {
    const selectedRows = gridOptions.api.getSelectedNodes();
    console.log(selectedRows)
}

 <AgGridReact
   onCellClicked={onCellClicked}
   ....
  >
 </AgGridReact>

Check working example

Usama
  • 1,038
  • 9
  • 22
0

I think you are trying to use onSelectionChanged out of its scope, if you need to see if a cell is clicked (any number of times, you can use the onCellClicked Event). there are multitudes of events that can be used if that doesn't suit your needs. https://www.ag-grid.com/react-data-grid/grid-events/#reference-selection

Otherwise if you are going to use this event only one handy way would be to change the selected row at the end of your processing and making this process async. this would be be problematic if the user clicks the row you have chosen to set it to so not the optimal solution i would say. https://www.ag-grid.com/react-data-grid/row-selection/#node-selection-api

Raghav
  • 41
  • 1
  • 5