Foo Stack
Foo Stack

Reputation: 2305

Getting select rows from ng-grid?

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)

Upvotes: 12

Views: 41866

Answers (5)

Atomic Star
Atomic Star

Reputation: 5507

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.

Upvotes: 4

genegc
genegc

Reputation: 1702

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

Upvotes: 3

ThanhHH
ThanhHH

Reputation: 6600

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

$scope.gridOptions.$gridScope.selectedItems

Upvotes: 7

PaulL
PaulL

Reputation: 6650

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

Upvotes: 1

Ye Liu
Ye Liu

Reputation: 8976

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>

Upvotes: 23

Related Questions