首页 > 解决方案 > 如何将焦点设置在 onRowClicked 行中的第一个可编辑单元格上?

问题描述

我有一个需要能够通过键盘进行编辑的 ag-grid。我将editType 设置为fullRow,我希望能够添加javascript,这样当我触发onRowEditingStarted 事件时,它会转到第一个可编辑单元格,因为有时我的网格比屏幕大。

我可以获取 rowIndex,但我无法获取列集合来查看属性。我想保留 rowIndex,但获取第一个可编辑列并在那里设置焦点。(现在,它是偶然选择触发事件的任何东西。)

有人有我可以看的例子吗?

我试过查看这里产生的对象:

onRowEditingStarted: function(params) {
   console.log("started row editing");
   console.log(params);
}

这给了我一个可以获取 rowIndex 和 columnApi 的事件。我希望这里的某个地方是一个执行 for 循环的集合,但我没有看到它。

标签: javascriptag-grid

解决方案


你可以在你的onRowEditingStarted

// scrolls to the first column, or first editable column. you can pass in the correct index
var firstEditCol = params.columnApi.getAllDisplayedColumns()[0];
/////
params.api.ensureColumnVisible(firstEditCol );

// sets focus into the first grid cell
params.api.setFocusedCell(params.rowIndex, firstEditCol);  

这个来自 docs 的例子是一个很好的起点


推荐阅读