Getting select rows from ng-grid?

旧城冷巷雨未停 提交于 2020-01-09 19:52:47

问题


How do I create (or access) an array of selected rows in my ng-grid?


Documentation (scroll to "Grid options")

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.

index.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 for the code (and to run it)


回答1:


Based on the doc, selectedItems should be a property of $scope.gridOptions, so try this:

Controller

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

HTML

<pre>{{gridOptions.selectedItems}}</pre>



回答2:


You can get selected items of ng-grid 2.x from:

$scope.gridOptions.$gridScope.selectedItems



回答3:


In version 3 you can do:

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

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

Refer to http://ui-grid.info/docs/#/api/ui.grid.selection.api:PublicApi for more info.




回答4:


For 3.0, you can capture rows as they're selected like this:

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

More info here: http://ui-grid.info/docs/#/tutorial/210_selection




回答5:


I'm attempting to read a list of selected rows at the moment. The option appears to have moved, I can now find this in:

$scope.gridOptions.ngGrid.config.selectedItems

It appears to be read-only



来源:https://stackoverflow.com/questions/16875283/getting-select-rows-from-ng-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!