12

如何在我的ng-grid中创建(或访问)一组选定行?


文档(滚动到“网格选项”)

id                 | default value | definition
-----------------------------------------------
selectedItems      |       []      | all of the items selected in the grid.
                                     In single select mode there will only
                                     be one item in the array.

索引.html

<body ng-controller="MyCtrl">
    <div class="gridStyle" ng-grid="gridOptions"></div>

    <h3>Rows selected</h3>
    <pre>{{selectedItems}}</pre>
</body>

main.js

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.myData = [{name: "Moroni", age: 50},
                     {name: "Tiancum", age: 43},
                     {name: "Jacob", age: 27},
                     {name: "Nephi", age: 29},
                     {name: "Enos", age: 34}];
    $scope.gridOptions = { data: 'myData' };
});

Plnkr 用于代码(并运行它)

4

5 回答 5

23

根据文档,selectedItems应该是 的属性$scope.gridOptions,所以试试这个:

控制器

$scope.gridOptions = { data: 'myData', selectedItems: [] };

HTML

<pre>{{gridOptions.selectedItems}}</pre>
于 2013-06-01T17:38:16.997 回答
7

您可以从以下位置获取 ng-grid 2.x 的选定项目:

$scope.gridOptions.$gridScope.selectedItems
于 2014-08-06T16:31:08.333 回答
4

在版本 3 中,您可以执行以下操作:

$scope.gridOptions.onRegisterApi = function(gridApi){

  $scope.gridApi = gridApi;
  $scope.mySelectedRows=$scope.gridApi.selection.getSelectedRows();
}

有关详细信息,请参阅http://ui-grid.info/docs/#/api/ui.grid.selection.api:PublicApi

于 2014-11-27T09:01:44.947 回答
3

对于 3.0,您可以在选择行时捕获行,如下所示:

$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);
  });
};

更多信息在这里:http ://ui-grid.info/docs/#/tutorial/210_selection

于 2014-10-05T13:15:28.700 回答
1

我目前正在尝试读取选定行的列表。该选项似乎已移动,我现在可以在以下位置找到它:

$scope.gridOptions.ngGrid.config.selectedItems

它似乎是只读的

于 2014-07-18T23:14:40.107 回答