1
votes

How to enter the grid by using the tab key ?

It is possible to set the tabindex on the html div declaring the tab but it will only focus the whole div. I would like to focus on the first cell of the first row.

Personaly I came up with the following hack but I am looking for a better solution. Any external lib is welcome.

I have only one column in my grid and simple rows, so this code should be improved to remove potential focus on all cells for all columns and/or for rows with more than one level.

Inside my directive

<div ag-grid="ctlr.gridOptions" class="ag-fresh" role="grid" tabindex="201" ng-focus="ctlr.onFocus()" id="myGrid"></div>

Inside controller

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

};

Existing issue on github : https://github.com/ceolter/ag-grid/issues/183

1

1 Answers

0
votes

Using a directive feels less hacky to me - after adding tabindex="0" to the grid div so it gets the keyboard focus I use this directive to move the focus from the div to the top left cell:

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