我正在从 JavaScript 中选择我的 ngGrid,调用gridOptions.selectItem()
. 我将 multiSelect 设置为 false,因此只选择了一行。我希望 ngGrid 自动滚动以显示新选择的行,但我不知道该怎么做:有人可以帮忙吗?
关于相关主题:我可以通过鼠标单击禁用行选择吗?如果是这样,怎么做?
编辑添加
如果可能的话,我还想禁用所选行的键盘导航。
什么有效:
AardVark71 的回答奏效了。我发现 ngGridngGrid
在gridOptions
变量上定义了一个属性,该属性包含对网格对象本身的引用。通过此对象的属性公开必要的功能:
$scope.gridOptions.selectItem(itemNumber, true);
$scope.gridOptions.ngGrid.$viewport.scrollTop(Math.max(0, (itemNumber - 6))*$scope.gridOptions.ngGrid.config.rowHeight);
我的网格固定在 13 行高,我的逻辑试图使选定的行出现在网格的中间。
如果可能的话,我仍然想禁用鼠标和键盘对选择的更改。
什么也有效:
这可能更接近“角度方式”并达到相同的目的:
// 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);
尽管通过单击选择一行时的效果可能有点令人不安。