3

I am trying to refresh ag grid and reselect the last row before update.

This is my AG grid component.

  onCellClicked = params => {console.log(params)
    let node = params.node;

    Promise.all([
      node.setSelected(true, true),
      this.props.actions.selectMarketingProspect(node.data),
      this.props.actions.getMarketingCustomerNotes(this.props.dealerID,node.data.id)
    ]);
  };

      <AgGridReact
          columnDefs={columnDefs}
          defaultColDef={{
            resizable: true,
            sortable: true
          }}
          rowSelection='single'
          getRowNodeId={data => data.id}
          deltaRowDataMode={true}
          frameworkComponents={frameworkComponents}
          rowClassRules={this.state.rowClassRules}
          detailCellRenderer={detailCellRenderer}
          onFirstDataRendered={this.onFirstDataRendered}
          onGridReady={this.onGridReady}
          onSelectionChanged={this.onSelectionChanged}
          onCellClicked={this.onCellClicked}
          sideBar={toolPanelOptions}
          onModelUpdated={this.onModelUpdated}
      />
    </div>

Any ideas?

Based on what I read on the docs I believe that node.setselected should do the trick but I cant quite get there. Any help will be appreciated.

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
chewie
  • 529
  • 4
  • 17

2 Answers2

0

You can keep track of the last selected RowNode ids by listening to the selection changed event:

const [selectedRowIds, setSelectedRowIds] = React.useState<string[]>([]);
<AgGridReact
  onSelectionChanged={(e) => {
    const selectedNodes = e.api.getSelectedNodes();
    if (selectedNodes.length === 0) return;

    const selectedRowIds = selectedNodes
      .map((n) => n.id)
      .filter(Boolean) as string[];

    setSelectedRowIds(selectedRowIds);
  }}
  {...}
/>

Then when you want to select it again, use GridApi.getRowNode(id) to get the RowNode you want to select and call RowNode.setSelected(true) to select it:

selectedRowIds.forEach((id) => {
  gridApi?.getRowNode(id)?.setSelected(true);
});

Codesandbox Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
  • Note that if your data has a new unique ID when you refresh, this won't work. You can use a similar concept though. – Ben in CA Jan 05 '23 at 17:46
0

I had the issue using the redux store. It was easy solved with the getRowId property. You can watch this on the 2:00 minute mark from the official ag-grid channel. https://www.youtube.com/watch?v=_V5qFr62uhY

  const getRowId = useCallback((params) => {
    return params.data.id;
  });