Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a node by index in ag-grid?

AgGrid expects node(s) to be passed in to lot of it's data functions. How do you get a node by index? Look at the snip below:

api.forEachNode(function(node){
   api.refreshRows([node]);
})

I can pass the node parameter to refreshRows() function since I'm getting it through forEachNode().

How do you get a node by index without iterating through forEachNode() ?

like image 809
Charlie Avatar asked Jan 11 '16 06:01

Charlie


People also ask

How do you get row node by row index in Ag grid?

Accessing Rows by Row ID The easiest way to get a Row Node is by its Row ID. The ID is either provided by you using the grid callback getRowId() , or generated by the grid using an internal sequence.

How do you get row index on Ag grid?

You can use getVirtualRow() method to get a single row. This function is a part of the Row Model. You can get the Row Model by getModel() function. Then be sure to disable sorting/filtering if you do that otherwise you'll have some problems.

How do you get filtered row count on Ag grid?

getDisplayedRowCount() as the name suggest returns total displayed rows. Initially, it returns all the rows. After filter, it returns filtered rows. @LittleXenomorph's answer is the best one.


3 Answers

This might be a bit late for this question, but anyway for people who are searching for this in the future :

Apart from the answers given, you can also get the row node by using the following ways,

// Getting the row node by the row index
cont rowNode1 = api.getDisplayedRowAtIndex(rowIndex);

In some cases, the above approach is not suitable because the rowIndex can get changed when you do some changes to your grid (sort, filter, etc.).
The other method is to use the id of the row which will not change even if you sort, filter... the grid.

getRowNode(id) : Returns the row node with the given ID. The row node id is the one you provided with the callback getRowNodeId(data), otherwise, the id is a number auto-generated by the grid when the row data is set.

// Getting rowNode by row id
const rowNode2 = api.getRowNode(rowId);
like image 143
Senal Avatar answered Oct 03 '22 23:10

Senal


You can use getVirtualRow() method to get a single row. This function is a part of the Row Model. You can get the Row Model by getModel() function.

var model = api.getModel();
console.log(model.getVirtualRow(idx));
like image 23
Charlie Avatar answered Oct 03 '22 22:10

Charlie


Building on @Charlie H's answer, it's entirely possible that since the version that he was using, the API has changed a bit. I'm using the (current as of December 2017) version 15.0. I found that rowsToDisplay[] contains an array of rows accessible. The following, for example, does exactly what you'd think it would:

onCellEditingStarted: function (event) {
    var displayModel = gridOptions.api.getModel();
    var rowNode = displayModel.rowsToDisplay[event.rowIndex];
    rowNode.setRowHeight(100);
    gridOptions.api.onRowHeightChanged();
},
like image 40
Bob Kaufman Avatar answered Oct 03 '22 23:10

Bob Kaufman