Getting select rows from ng-grid?
Asked Answered
D

5

12

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)

Daggna answered 1/6, 2013 at 17:27 Comment(0)
J
23

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>
Janycejanyte answered 1/6, 2013 at 17:38 Comment(1)
Thanks, had thought it was a default option; due to it having a default value; guess I won't expect that again. It's working :)Daggna
G
7

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

$scope.gridOptions.$gridScope.selectedItems
Grandchild answered 6/8, 2014 at 16:31 Comment(0)
L
4

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.

Loiretcher answered 27/11, 2014 at 9:1 Comment(1)
I had multiple grids on my page and it seemed to cause problems with setting the $scope.mySelectedRows, even with unique values, I am not sure why as it should work fine. I was however still able to get to each grid using $scope.uniqueGridApi.selection.getSelectedRows().Lulu
C
3

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

Cohette answered 5/10, 2014 at 13:15 Comment(1)
Although this is an acceptable solution, I prefer the answer from Ewald Stieger because it has less code to maintain.Lulu
H
1

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

Hubblebubble answered 18/7, 2014 at 23:14 Comment(1)
This was a life saver for me. The selectedItems property I tried to set this to was not working -- this does!Michaelmichaela

© 2022 - 2024 — McMap. All rights reserved.