如何使用Tab键输入网格?
可以在html div上设置tabindex来声明选项卡,但它只会聚焦整个div。我想专注于第一行的第一个单元格。
Personaly我提出了以下黑客攻击,但我正在寻找更好的解决方案。欢迎使用任何外部库。
我的网格中只有一列和简单的行,因此应该改进此代码以消除对所有列和/或具有多个级别的行的所有单元格的潜在焦点。
在我的指令
中<div ag-grid="ctlr.gridOptions" class="ag-fresh" role="grid" tabindex="201" ng-focus="ctlr.onFocus()" id="myGrid"></div>
内部控制器
ctrl.onFocus = function()
{
var rows = $('#myGrid .ag-row.ag-row-even.ag-row-level-0');
var firstRow = rows[0];
var firstColumnCells = $('#myGrid .ag-row.ag-row-even.ag-row-level-0 .ag-cell.cell-col-0');
var firstCell = firstColumnCells[0];
//remove potential focus on rows
for (var i = 0; i < rows.length; i++)
{
rows[i].classList.remove('ag-row-focus');
rows[i].classList.add('ag-row-no-focus');
}
//remove potential focus on first column cells
for (var j = 0; j < rows.length; j++)
{
firstColumnCells[j].classList.remove('ag-cell-focus');
firstColumnCells[j].classList.add('ag-cell-no-focus');
}
//focus first row
firstRow.classList.remove('ag-row-no-focus');
firstRow.classList.add('ag-row-focus');
//focus first cell
firstCell.classList.remove('ag-cell-no-focus');
firstCell.classList.add('ag-cell-focus');
firstCell.focus();
};
github上存在的问题:https://github.com/ceolter/ag-grid/issues/183
答案 0 :(得分:0)
使用指令对我来说感觉不那么笨拙 - 在将tabindex =“0”添加到网格div后,它获得键盘焦点我使用此指令将焦点从div移动到左上角单元格:
function gridTab($window) {
return {
restrict : 'A',
link : function(scope, element) {
element.bind('focus', function() {
if (scope.grid.rowData.length > 0) {
scope.grid.api.setFocusedCell(0,0);
}
});
}
};
}