Where can I get Angular ui-grid selected items
Asked Answered
U

4

37

Testing out Angular ui-grid (ng-grid v.3.0). Can not for the life of me find the selected row. I just want to grab the rows or even row ID of row when a user clicks it. Found the top comment here but I think this is outdated: Getting select rows from ng-grid?

Does anyone know where the gridOptions.selectedItems is being stored in 3.0?

Unconsidered answered 4/10, 2014 at 0:50 Comment(0)
E
36

Is this what your are looking for ? http://ui-grid.info/docs/#/tutorial/210_selection

  1. Activate grid selection capabilities with the ui-grid-selection tag (and ui.grid.selection module registration in your app
  2. register gridApi and use gridApi.selection to access getSelectedRows()
Equilateral answered 4/10, 2014 at 1:33 Comment(0)
F
24

In addition to the steps above https://mcmap.net/q/414802/-where-can-i-get-angular-ui-grid-selected-items, you might have to invoke it through a ng-click event to get the actual value/object. At least that's how I had it working.

Eg:
$scope.selectRow = function(){
    $scope.gridApi.selection.getSelectedRows();
};

And call selectRow() from the template.

This is for anybody who have been confused like I did, considering the fact that ui-grid does not have the best documentation (specially for this select portion).

Froude answered 1/4, 2015 at 18:33 Comment(1)
Thanks Rajush, I appreciate the extra info!Unconsidered
B
16

The easiest approach is:

  1. Register the gridApi by adding this your controller:

    $scope.gridOptions.onRegisterApi = function(gridApi) { $scope.myGridApi = gridApi; };

  2. Access the array of selected items:

    $scope.myGridApi.selection.getSelectedRows();

Benthos answered 31/8, 2015 at 16:34 Comment(0)
O
0

With grid ui you have to use the selection.on.rowSelectionChanged to update a scope variable that store the selectedItem. In this way you can use the value in a binding expression.

var SelectController = function($scope) {
    ...
    $scope.selectedItem = null;

    $scope.gridOptions = {
            data : 'articles',
            enableRowSelection : true,
            multiSelect : false,
            enableRowHeaderSelection : false,
            ...
        };

        $scope.gridOptions.onRegisterApi = function(gridApi) {
            // set gridApi on scope
            this.$scope.gridApi = gridApi;
        }.bind(this);
        $scope.gridOptions.onRegisterApi = function(gridApi) {
            // set gridApi on scope
            this.$scope.gridApi = gridApi;
            this.$scope.gridApi.selection.on.rowSelectionChanged($scope,
                    function(row) {
                        this.$scope.selectedItem = row.entity;
                    }.bind(this));
        }.bind(this);

Use a an array instead of a plain object if you need multiple selection.

Occultation answered 22/5, 2015 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.