如何在Angular UI网格中选择下一行?

时间:2015-04-09 16:44:24

标签: javascript angularjs

我有一个选定的行,通过点击某个按钮(目前我通过AngularHotkeys.js使用空格)我想取消选择当前行并选择当前所选行之后的下一行。

事情很复杂,因为我知道我可以用不同的列对表进行排序。因此,了解应用当前排序的当前行的索引会很棒。

从哪里开始解决这个问题?
任何建议都表示赞赏。

2 个答案:

答案 0 :(得分:2)

您可以从$scope.gridApi.grid.renderContainers.body.visibleRowCache获取排序和过滤状态的行数组。当你拥有实体并拥有gridRow时,还有一堆棘手的处理方式,因此代码变得有点复杂。

您的代码类似于:

$scope.selectNextRow = function() {
  var currentRowIndex;
  var selectedRows = $scope.gridApi.selection.getSelectedRows();
  if( selectedRows.length < 1 ){
    // if nothing selected, we'll select the top row
    currentRowIndex = -1;
  } else {
    // if there are multiple selected, we use the first one
    var selectedRow = selectedRows[0];
    var gridRow = $scope.gridApi.grid.getRow(selectedRow);
    currentRowIndex = $scope.gridApi.grid.renderContainers.body.visibleRowCache.indexOf(gridRow);
  }

  $scope.gridApi.selection.clearSelectedRows();
      $scope.gridApi.selection.selectRow($scope.gridApi.grid.renderContainers.body.visibleRowCache[currentRowIndex + 1].entity);
};

参考http://plnkr.co/edit/Z7HCjVY6oxGJzyjLI6Qo?p=preview

答案 1 :(得分:-1)

如果我理解你的话,你应该能够用这样的东西完成你想要的东西。 (没有测试)

$scope.scrollTo = function( rowIndex ) {
    $scope.gridApi.core.scrollTo( $scope.gridOptions.data[rowIndex], 0);
};

$scope.nextRow() {
   var current = $scope.gridApi.selection.getSelectedRows();
   scrollTo(current + 1);
}

干杯