Angular ui-grid 3.0 获取选中行

Angular ui-grid 3.0 get selected rows

This answer 声明此代码:

$scope.gridOptions.onRegisterApi = function(gridApi){
  $scope.gridApi = gridApi;
  $scope.mySelectedRows=$scope.gridApi.selection.getSelectedRows();
}

应该可以获取选定的行,但对我来说 returns 总是 [],要跟踪选定的行我必须在每次触发选择事件时调用 gridApi.selection.getSelectedRows() , 这是正确的吗?

我想要实现的是做我自己的页脚来跟踪网格的选定行数,这是实现此目的的正确方法吗?

已经有一个在页脚中显示所选元素数量的示例。

此 plnkr 显示所选项目的页脚。 http://plnkr.co/edit/jc1YPCXBmfOKWyu8sLkx?p=preview

如果您想对所选行进行进一步分析,您可以为行选择注册一个侦听器并对其进行操作。

 $scope.gridOptions.onRegisterApi = function(gridApi){
      //set gridApi on scope
      $scope.gridApi = gridApi;
      gridApi.selection.on.rowSelectionChanged($scope,function(row){
        var msg = 'row selected ' + row.isSelected;
        $log.log(msg);
      });

      gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
        var msg = 'rows changed ' + rows.length;
        $log.log(msg);
      });
    };

我可以正常工作 w/o 必须使用事件触发器。我添加了一个功能,将它绑定到一个按钮,并且仅在我需要时才可以检索选定的项目。

$scope.gridOptions = {
  data: 'data',
  enableRowSelection: true,
  onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
    $scope.gridApi = gridApi;
  }
};
//this is the on click function
$scope.getCurrentSelection = function() {
  var currentSelection = $scope.gridApi.selection.getSelectedRows();
  console.log(currentSelection);
};

This function is work for me when select All function activated i have got the all selected rows.

gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
                    console.log(row);
                  });
$scope.gridOptions = {
  data: 'data',
  enableRowSelection: true,
  onRegisterApi: function(gridApi) { //register grid data first within the gridOptions
    $scope.gridApi = gridApi;
  }
};

在模板html中直接访问变量{{vm.gridApi.selection.getSelectedRows().length}}后