monkeyWithAMachinegun
monkeyWithAMachinegun

Reputation: 331

Ag-grid not keeping row focus on table refresh

I have an ag-grid table that refreshes its data every few seconds. Whenever it does this, the selected table row loses focus and I'm trying to figure out a way to force the focus back. I thought it would be as simple as keeping track of the selected node, then setting the values when the refresh occurs but it doesn't seem to be working. Basically, whenever the user makes a selection in the table, I track the node and the focused cell:

function onSelectionChanged() {
    var selectedRowNode = $scope.ResourceCheckoutTableGrid.api.getSelectedNodes()[0];
    $scope.focusedCell = $scope.ResourceCheckoutTableGrid.api.getFocusedCell();
    $scope.selectedRowNode = selectedRowNode;   
}

Then, I load the table data every three seconds:

$scope.startRefresh = function() {
    setInterval(function() {
        $scope.loadTableData();
    },3000)
}

When the table data is reloaded it loses the focus, but I try and bring it back using the saved values:

$scope.loadTableData = function() {
    $http.get('RobotDataInterface/Resource/getAllCheckedOutResources').success(
            function(data) {
                $scope.rowCount = data.length;
                if ($scope.ResourceCheckoutTableGrid.api != undefined) {
                    $scope.ResourceCheckoutTableGrid.api.setRowData(data);
                    $scope.ResourceCheckoutTableGrid.api.sizeColumnsToFit();
                    var node = $scope.selectedRowNode;
                    var cell = $scope.focusedCell;
                    if (node != "" && cell != "") {
                        node.setSelected(true);
                        $scope.ResourceCheckoutTableGrid.api.setFocusedCell(cell.rowIndex, cell.column);
                    }
                }
            }); 
};

If I set a breakpoint, I can see that when it makes these calls:

node.setSelected(true);
$scope.ResourceCheckoutTableGrid.api.setFocusedCell(cell.rowIndex, cell.column);

I can see that the values are what I expect; the node and the focused cell, rowIndex etc are what I expect, but still the selected row loses focus (even though it actually does get set to 'selected = true'. Does anyone know what I'm doing wrong?

Upvotes: 2

Views: 4191

Answers (2)

monkeyWithAMachinegun
monkeyWithAMachinegun

Reputation: 331

Okay it looks like this works for what I was trying to do; rather than the cell focus I needed to set the node itself to selected = true...I could swear I'd done this earlier and even verified it was setting selected to true, but either way this bit of code accomplishes the desired result:

                    $scope.ResourceCheckoutTableGrid.api.forEachNode((node) => {
                        if (node.childIndex === ($scope.selectedRowNode.childIndex)) {
                            node.setSelected(true);
                            return;
                        }
                    });

Upvotes: 0

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, change cell.column to cell.column.colId in setFocusedCell method

Upvotes: 1

Related Questions