我如何滚动一个ngGrid显示当前选择? [英] How do I scroll an ngGrid to show the current selection?
问题描述
我从JavaScript设置我ngGrid的选择,称 gridOptions.selectItem()
。我多选设置为false,所以永远都只有一个选定一行。我想了ngGrid自动滚动显示新选定的行,但是我不知道如何做到这一点?谁能帮助,请
I'm setting the selection of my ngGrid from JavaScript, calling gridOptions.selectItem()
. I have multiSelect set to false, so there is only ever one row selected. I'd like the ngGrid to automatically scroll to show the newly selected row, but I don't know how to do this: can anyone help, please?
在一个相关的话题:我可以通过鼠标禁用行选择点击?如果是这样,怎么样?
On a related topic: can I disable row selection by mouse click? If so, how?
编辑补充
我也想停用选定行的键盘导航,如果可能的话。
I'd also like to disable keyboard navigation of the selected row, if possible.
什么工作:
AardVark71的答复工作。我发现ngGrid定义上持有到网格对象本身的引用 gridOptions
变量属性 ngGrid
。必要的功能都通过此对象的属性公开:
AardVark71's answer worked. I discovered that ngGrid defines a property ngGrid
on the gridOptions
variable which holds a reference to the grid object itself. The necessary functions are exposed via properties of this object:
$scope.gridOptions.selectItem(itemNumber, true);
$scope.gridOptions.ngGrid.$viewport.scrollTop(Math.max(0, (itemNumber - 6))*$scope.gridOptions.ngGrid.config.rowHeight);
我的网格被固定在13行高,和我的逻辑试图使所选择的行出现在网格的中间
My grid is fixed at 13 rows high, and my logic attempts to make the selected row appear in the middle of the grid.
我还是想禁用鼠标和放大器;如果可能的话键盘改变为选择。
I'd still like to disable mouse & keyboard changes to the selection, if possible.
什么还曾:
这可能是更接近'角路,达到同样的目的:
This is probably closer to the 'Angular Way' and achieves the same end:
// 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);
虽然当一个行通过单击所选的效果可能会有点不安。
although the effect when a row is selected by clicking on it can be a bit disconcerting.
推荐答案
这听起来像你可以使用 scrollTop的作为滚动的方法。
It sounds like you can make use of the scrollTop method for the scrolling.
又见<一个href=\"http://github.com/angular-ui/ng-grid/issues/183\">http://github.com/angular-ui/ng-grid/issues/183从@布莱恩 - 瓦以下的plunker http://plnkr.co/edit/oyIlX9?p=$p $ PVIEW
See also http://github.com/angular-ui/ng-grid/issues/183 and the following plunker from @bryan-watts http://plnkr.co/edit/oyIlX9?p=preview
这是例子,说明这可能是工作将如下所示:
An example how this could work would be as follows:
function focusRow(rowToSelect) {
$scope.gridOptions.selectItem(rowToSelect, true);
var grid = $scope.gridOptions.ngGrid;
grid.$viewport.scrollTop(grid.rowMap[rowToSelect] * grid.config.rowHeight);
}
结果
修改
对于你的问题禁用所选行的鼠标和键盘事件的第二部分,它可能是最好的开始一个新问题。听起来像是要动态设置enableRowSelection假?不知道如果这是可能的。
For the second part of your question "disabling the mouse and keyboard events of the selected rows" it might be best to start a new Question. Sounds like you want to set your enableRowSelection dynamically to false? No idea if that's possible.
这篇关于我如何滚动一个ngGrid显示当前选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!