我正在从 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);
尽管通过单击选择一行时的效果可能有点令人不安。