简体   繁体   English

如何在ag-grid中通过索引获取节点?

[英]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. AgGrid 期望将节点传递给它的许多数据函数。 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() .我可以将node参数传递给refreshRows()函数,因为我是通过forEachNode()获取它的。

How do you get a node by index without iterating through forEachNode() ?如何在不遍历forEachNode()的情况下通过索引获取节点?

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.).在某些情况下,上述方法并不适用,因为当您对网格进行一些更改(排序、筛选等)时,rowIndex 可能会发生变化。
The other method is to use the id of the row which will not change even if you sort, filter... the grid.另一种方法是使用行的 id,即使你排序,过滤......网格也不会改变。

getRowNode(id) : Returns the row node with the given ID. getRowNode(id) :返回具有给定 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.行节点 id 是您通过回调 getRowNodeId(data) 提供的,否则,id 是设置行数据时网格自动生成的数字。

// Getting rowNode by row id
const rowNode2 = api.getRowNode(rowId);

You can use getVirtualRow() method to get a single row.您可以使用getVirtualRow()方法获取单行。 This function is a part of the Row Model.此功能是行模型的一部分。 You can get the Row Model by getModel() function.您可以通过getModel()函数获取行模型。

var model = api.getModel();
console.log(model.getVirtualRow(idx));

Building on @Charlie H's answer, it's entirely possible that since the version that he was using, the API has changed a bit.基于@Charlie H 的回答,完全有可能自从他使用的版本以来,API 发生了一些变化。 I'm using the (current as of December 2017) version 15.0.我正在使用(截至 2017 年 12 月的当前版本)15.0. I found that rowsToDisplay[] contains an array of rows accessible.我发现rowsToDisplay[]包含可访问的行数组。 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();
},

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.grid 是你的网格名称):

        $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):或者,如果你想要一个按子索引的行节点,你现在可以使用(在上面设置 $scope.rows 之后):

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

Where i is the row number you want.我是你想要的行号。

AgGrid expects node(s) to be passed in to lot of it's data functions. AgGrid希望将节点传递到其许多数据功能中。 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() .我可以将node参数传递给refreshRows()函数,因为我是通过forEachNode()获得它的。

How do you get a node by index without iterating through forEachNode() ?如何在不迭代forEachNode()情况下按索引获取节点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM