如何滚动 ngGrid 以显示当前选择?

2024-05-04

我正在从 JavaScript 设置 ngGrid 的选择,调用gridOptions.selectItem()。我将 multiSelect 设置为 false,因此只选择了一行。我希望 ngGrid 自动滚动以显示新选择的行,但我不知道该怎么做:有人可以帮忙吗?

关于相关主题:我可以通过鼠标单击禁用行选择吗?如果是这样,怎么办?

编辑添加

如果可能的话,我还想禁用所选行的键盘导航。

什么有效:

AardVark71 的答案有效。我发现 ngGrid 定义了一个属性ngGrid on the gridOptions保存对网格对象本身的引用的变量。必要的功能通过该对象的属性公开:

$scope.gridOptions.selectItem(itemNumber, true);
$scope.gridOptions.ngGrid.$viewport.scrollTop(Math.max(0, (itemNumber - 6))*$scope.gridOptions.ngGrid.config.rowHeight);

我的网格固定为 13 行高,我的逻辑尝试使所选行出现在网格的中间。

如果可能的话,我仍然想禁用鼠标和键盘对选择的更改。

还有效的方法:

这可能更接近“Angular Way”并达到相同的目的:

// This $watch scrolls the ngGrid to show a newly-selected row as close to the middle row as possible
$scope.$watch('gridOptions.ngGrid.config.selectedItems', function (newValue, oldValue, scope) {
            if (newValue != oldValue && newValue.length > 0) {
                var rowIndex = scope.gridOptions.ngGrid.data.indexOf(newValue[0]);
                scope.gridOptions.ngGrid.$viewport.scrollTop(Math.max(0, (rowIndex - 6))*scope.gridOptions.ngGrid.config.rowHeight);
            }
        }, true);

尽管通过单击选择一行时的效果可能有点令人不安。


听起来你可以利用滚动顶部滚动的方法。

也可以看看http://github.com/angular-ui/ng-grid/issues/183 http://github.com/angular-ui/ng-grid/issues/183以及来自 @bryan-watts 的以下笨蛋http://plnkr.co/edit/oyIlX9?p=preview http://plnkr.co/edit/oyIlX9?p=preview

其工作原理示例如下:

function focusRow(rowToSelect) {
  $scope.gridOptions.selectItem(rowToSelect, true);
  var grid = $scope.gridOptions.ngGrid;
  grid.$viewport.scrollTop(grid.rowMap[rowToSelect] * grid.config.rowHeight);
}


edit:

对于问题的第二部分“禁用所选行的鼠标和键盘事件”,最好开始一个新问题。听起来您想将enableRowSelection动态设置为false?不知道这是否可能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何滚动 ngGrid 以显示当前选择? 的相关文章

随机推荐