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

36,548

Solution 1

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));

Solution 2

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);

Solution 3

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();
},

Solution 4

If you want to iterate through the grid row by row in order you can use this (where $scope.grid is your grid name):

        $scope.high_index = 0;
        $scope.rows = [];

        $scope.grid.api.forEachNode(function (node) {

            $scope.rows[node.childIndex] = node.id;

            if (node.childIndex > $scope.high_index)
                $scope.high_index = node.childIndex;
        });

        for (i = 0; i <= $scope.high_index; i++) {

            var node = $scope.grid.api.getRowNode($scope.rows[i]);
        }

Or if you want a row node by child index you can now use (after setting up $scope.rows above):

       var node = $scope.grid.api.getRowNode($scope.rows[i]);

Where i is the row number you want.

Share:
36,548
Charlie
Author by

Charlie

Please leave a comment with your down votes. So, I can make sure you are not disappointed next time.

Updated on July 09, 2022

Comments

  • Charlie
    Charlie almost 2 years

    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() ?