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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34715290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:46:31  来源:igfitidea点击:

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

javascriptangularjsag-grid

提问by Charlie

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:

AgGrid 期望将节点传递给它的许多数据函数。如何通过索引获取节点?看看下面的截图:

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

I can pass the nodeparameter 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()

回答by Charlie

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.

您可以使用getVirtualRow()方法来获取单行。此函数是行模型的一部分。您可以通过getModel()函数获取行模型。

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

回答by Senal

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

对于这个问题,这可能有点晚了,但无论如何对于将来正在寻找这个问题的人来说:

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

除了给出的答案,您还可以通过以下方式获取行节点,

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

For some cases above approach is not suitable since the rowIndex could change 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.

对于某些情况,上述方法不适合,因为当您对网格(排序、过滤器等)进行一些更改时,rowIndex 可能会更改。
另一种方法是使用即使您排序、过滤...网格也不会改变的行的 id。

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

getRowNode(id):返回具有给定 ID 的行节点。行节点 id 是您通过回调 getRowNodeId(data) 提供的,否则 id 是设置行数据时由网格自动生成的数字。

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

回答by Bob Kaufman

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:

基于@Charlie H 的回答,完全有可能自从他使用的版本以来,API 已经发生了一些变化。我正在使用(截至 2017 年 12 月)版本 15.0。我发现它rowsToDisplay[]包含一组可访问的行。例如,以下内容完全符合您的想法:

onCellEditingStarted: function (event) {
    var displayModel = gridOptions.api.getModel();
    var rowNode = displayModel.rowsToDisplay[event.rowIndex];
    rowNode.setRowHeight(100);
    gridOptions.api.onRowHeightChanged();
},

回答by Walfrat

You can't.

你不能。

This is because the grid is designed to filters/sort data. So working by index does not mean anything and is not supported natively.

这是因为网格旨在过滤/排序数据。因此,按索引工作并不意味着什么,并且本机不支持。

Why do you need to refresh the n-th rows ? If it's because of a user action you can catch the event, get the node in and call refresh. (link to event documentation : http://ag-grid.com/javascript-grid-events/index.php)

为什么需要刷新第 n 行?如果是因为用户操作,您可以捕获该事件,请获取节点并调用刷新。(链接到事件文档:http: //ag-grid.com/javascript-grid-events/index.php

If you don't have too much data (rows/columns) you can just call a global refresh.

如果您没有太多数据(行/列),您可以调用全局刷新。