How do I specify the RowNode.id in ag-grid
Asked Answered
E

4

9

I'm posting this here because this wasted a fair bit of my time today.

I was trying to set the RowNode.id from the data in the objects I set aggrid's rowData property to. meaning, I wanted to use a property on my data model to supply the built in id field of ag-grid's row model. They mention this in the documentation but they don't explain how to do itp.

Expeller answered 22/3, 2018 at 21:14 Comment(0)
E
6

The answer to this question is that you need to set the getRowNodeId property on the grid options object for the grid, like so:

// where 'd' is an object in your model
this.gridOptions.getRowNodeId = d => {
   return d.id; // return the property you want set as the id.
}
Expeller answered 22/3, 2018 at 21:14 Comment(0)
S
7

To get ag-grid to use application assigned IDs, implement the grid callback getRowNodeId(). The callback should return back the ID for a particular piece of row data. For example, the following code snippet returns back the value of attribute 'id' for the supplied data item:

function getRowNodeId(data) {
    return data.id;
}

When providing IDs the following rules must be obeyed:

  1. IDs must be unique
  2. IDs must not change

If the attribute you are intending to use as an ID is either not unique or changes, it will cause unspecified behaviour in the grid. In other words, don't use a field that is not unique or can change.

If using Row Grouping, the grid will always assign IDs for the group level (as there is not a one-to-one mapping with application-supplied row data). The callback getRowNodeId() is only used for non-group level rows.

Here's a link to documentation

Shivery answered 20/8, 2020 at 23:40 Comment(2)
with example, here -> ag-grid.com/javascript-data-grid/data-update-single-row-cellPollinate
API changed to getRowId({data}) in version 27.1.0, see ag-grid.com/javascript-data-grid/row-ids, ag-grid.com/changelog/?fixVersion=27.1.0Masturbation
E
6

The answer to this question is that you need to set the getRowNodeId property on the grid options object for the grid, like so:

// where 'd' is an object in your model
this.gridOptions.getRowNodeId = d => {
   return d.id; // return the property you want set as the id.
}
Expeller answered 22/3, 2018 at 21:14 Comment(0)
K
3

They actually not just mention about it, but also have a good example:

// callback tells the grid to use the 'id' attribute for id's
// id's should always be strings
gridOptions.getRowNodeId = function(data) {
    return data.id;
};

// get the row node with ID 55
var rowNode = api.getRowNode('55');

// do something with the row, eg select it
rowNode.setSelected(true);

Here is the link to documentation.

Kerin answered 18/4, 2019 at 21:0 Comment(0)
K
0
<ag-grid-angular
    class="ag-theme-balham"
    (gridReady)="onGridReady($event)"
    [columnDefs]="colDefs"
    [getNodeRowId]="getNodeRowId"
    [defaultColDef]="defaultColDef"
    (cellClicked)="onRowSelectionChanged($event)"
    (filterChanged)="onFilterChanged($event)"
    (gridSizeChanged)="onGridSizeChanged($event)"
  >
  </ag-grid-angular>

Find and Replace all GetRowNodeId to GetRowId, Name has been changed in Ag-grid ^30 version.

Keyek answered 5/3, 2024 at 6:1 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Citrine

© 2022 - 2025 — McMap. All rights reserved.