How do I scroll an ngGrid to show the current selection?

前端 未结 4 1665
甜味超标
甜味超标 2021-02-04 13:15

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.

4条回答
  •  庸人自扰
    2021-02-04 14:19

    I believe I was looking for the same behavior from ng-grid as yourself. The following function added to your gridOptions object will both disallow selection via the arrow keys (but allow it if shift or ctrl is held down) and scroll the window when moving down the list using the arrow keys so that the currently selected row is always visible:

    beforeSelectionChange: function(rowItem, event){
        if(!event.ctrlKey && !event.shiftKey && event.type != 'click'){
          var grid = $scope.gridOptions.ngGrid;
          grid.$viewport.scrollTop(rowItem.offsetTop - (grid.config.rowHeight * 2));
          angular.forEach($scope.myData, function(data, index){
            $scope.gridOptions.selectRow(index, false);
          });
        }
        return true;
      },
    

    edit: here is a plunkr: http://plnkr.co/edit/xsY6W9u7meZsTJn4p1to?p=preview

    Hope that helps!

提交回复
热议问题