ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Not able to get the selected rows in the UI-Grid.

Open siddhsuper opened this issue 11 years ago • 10 comments

Hi,

I am new to the ui-grid, trying to get the selected item from the ui-grid, but not able to get the selected item.

$scope.gridRolesList.onRegisterApi = function(gridApi){
                  //set gridApi on scope
                  $scope.gridApi = gridApi;
                  $scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();
                  gridApi.selection.on.rowSelectionChanged($scope,function(row){
                    console.log('row selected ' + row.isSelected);
                  });

                  //var g = $scope.gridApi.selection.getSelectedRows();   
                  gridApi.selection.on.rowSelectionChangedBatch($scope,function(rows){
                    console.log('rows changed ' + rows.length);
                  });
                };  

gridApi is showing undefined, and that's the reason the $scope.gridApi = gridApi; $scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows(); is not getting set.

siddhsuper avatar Apr 19 '15 15:04 siddhsuper

What other errors do you have on your javascript console? Is the grid rendering at all? Where in your code is this function - is it part of grid initialisation before the grid initially renders?

PaulL1 avatar Apr 19 '15 19:04 PaulL1

There are no other errors. Yeah Grid is rendering properly. And I am able to select the rows but not able to get the selected rows. Below are the piece of code which I am calling before the onRegisterApi.

                $scope.gridRolesList.rowHeight = 50;
                $scope.gridRolesList.enableSorting = false;
                $scope.gridRolesList.enableFiltering = false;
                $scope.gridRolesList.data = rolesCollection;
                $scope.gridRolesList.multiSelect = true;
                $scope.gridRolesList.enableRowSelection = true,
                $scope.gridRolesList.enableSelectAll = true,                
                //$scope.gridRolesList.showGridFooter = true,
                $scope.gridRolesList.columnDefs = [                    
                    { name: 'roleName', displayName: 'Name' },
                    { name: 'Actions',
                    cellTemplate: '<button class="btn btn-primary" ng-click="grid.appScope.EditRole(row.entity.roleName)">Edit</button><button class="btn btn-primary" ng-click="grid.appScope.DisableRoles(row.entity.roleName)">Disable</button>'
                }];

siddhsuper avatar Apr 20 '15 06:04 siddhsuper

It's working for me here: http://plnkr.co/edit/JNID2STlF0QELj2dpqzZ?p=preview

Just to be clear - are you getting the selected rows as soon as the grid has rendered (in the onRegisterApi)? Surely there won't be any rows selected at that point? Don't you have to wait until the user selects some rows before getting them?

PaulL1 avatar Apr 20 '15 09:04 PaulL1

A big thank you @PaulL1 .

As I said onRegisterApi was not getting initialized, reason was I had written it inside $http.get() call. Hence, gridApi was not getting initialized.

Now, it's working great.

siddhsuper avatar Apr 20 '15 10:04 siddhsuper

HI PaulL, I am facing the same issue with UIGrid in SharePoint 2013, I am using UI-Grid to select few Items in the grid to have certain action on it, but couldn't get the selected rows by using the function mentioned in your sample above, even though I've downloaded the same "ui-grid-unstable.js", "ui-grid-unstable.css" from the sample and almost copied the same steps of configuring the grid and how populate it with data, in addition to the note of initializing gridAPi mentioned by siddhusper, all that and I am still unable to get selected rows by: $scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();

so I used another method: gridApi.selection.on.rowSelectionChanged($scope, function (row) { ..} to get each row and add it to my selectedrows = [];

its working fine with an issue that I couldn't handle when the ticker button of the Grid clicked for "Select All/Deselect All" , so I can reset my SelectedRows[] Array. any help or advice is really appreciated.

Basel78 avatar Jul 01 '15 03:07 Basel78

@Basel78 you should read the documentation of ui.grid.selection. The secret is rowSelectionChangedBatch()

gridApi.selection.on.rowSelectionChanged(scope, singleSelect)
gridApi.selection.on.rowSelectionChangedBatch(scope, function (rows) {
    rows.forEach(singleSelect)
})

lpand avatar Jul 01 '15 07:07 lpand

Thanks lPand for the reply, Actually, the rowSelectionChangedBatch() is not fired in my case, so is there a property needs to be enabled except , enableSelectionBatchEvent : true. and where the event handler should comes in the code, as you mentioned its falling inside "gridApi.selection.on.rowSelectionChanged() {}" , but as for the documentations it's coming after it, and inside "$scope.gridOptions.onRegisterApi= " exactly like rowSelectionChanged().

it seems that I have problem to catch rowSelectionChangedBatch function in my code, since the event is not fired for some reason, can you please describe how to do it in code.

I will put part of the code can explain to me what I did wrong?


$scope.gridOptions = {
        enableRowSelection: true,
        enableSelectAll: true,
        selectionRowHeaderWidth: 35,
        rowHeight: 35,
        showGridFooter: true,
        enableSelectionBatchEvent: true
};
$scope.gridOptions.columnDefs = [
      { field: 'ProductID', displayName: 'Product ID' },
      { field: 'ProductName', displayName: 'Product Name' },
      { field: 'SupplierID', displayName: 'Supplier ID' },
      { field: 'CategoryID', displayName: 'Category ID' },
      { field: 'QuantityPerUnit', displayName: 'Quantity Per Unit' },
      { field: 'UnitsOnOrder', displayName: 'Units On Order' }
    ];
$scope.info = {};
$scope.getSelectedRows = function () {
         $scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();
};
    $scope.gridOptions.onRegisterApi = function (gridApi) {
        $scope.gridApi = gridApi;
       
        gridApi.selection.on.rowSelectionChanged($scope, function (row) {
            $scope.SelectedProduct = row.entity; 
      });
        gridApi.selection.on.rowSelectionChangedBatch($scope, function (rows) {
            rows.forEach($scope.SelectedProduct);
        });
    };

Basel78 avatar Jul 01 '15 15:07 Basel78

After deep checking on my case, I found out that I am two UI grid in my page, with same

 $scope.gridApi = gridApi;

so what is happening is a conflict BTW the gridApi on both grids, and when I checked the items on the first gird , the gridApi is working on the second one that is having mo items yet :)

anyway I Lesson here is we should use different "$scope.gridApi" for different grids on the same page!

Thanks lpand for your help.

Basel78 avatar Jul 02 '15 20:07 Basel78

You should really do $scope.gridApi1 and $scope.gridApi2 if you have two grids in the same controller.

PaulL1 avatar Jul 02 '15 20:07 PaulL1

how can I highlight ui-grid row on selection/click, which has 6 pinned columns?

piyush-avantsoft avatar Sep 13 '16 06:09 piyush-avantsoft