简体   繁体   English

Ag-Grid不将行重点放在表刷新上

[英]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? 节点和焦点单元格,rowIndex等是我所期望的,但是所选行仍然失去焦点(即使实际上确实设置为“ selected = true”。有人知道我在做什么吗?

为了获得预期的结果, 在setFocusedCell方法中将 cell.column更改为cell.column.colId

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: 而不是单元格焦点,我需要将节点本身设置为selected = true ...我可以发誓,我之前已经做过,甚至还验证了将其设置为true,但是无论哪种方式,这段代码都能达到预期的结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM