nir
nir

Reputation: 121

UI-GRID - Remove selected rows after row filtering

How can I deselect rows when these rows are filtered from the grid?
Is there any flag in angular-ui-grid that can do this job? Something like:

 gridOptions.removeSelectedAfterFilterd = true

Upvotes: 2

Views: 1302

Answers (2)

nir
nir

Reputation: 121

like an answer of Tim Harker without need add "$timeout"

var app = angular.module('app', ['ui.grid', 'ui.grid.selection']);
app.controller('MainCtrl', ['$scope', '$timeout',
  function($scope, $timeout) {
    $scope.gridOptions = {
      enableFiltering: true,
      enableSelectAll: true,
      enableRowSelection: true,
      enableRowHeaderSelection: false,
      enableFullRowSelection: true,
      showGridFooter: true,
      onRegisterApi: function(gridApi) {
        $scope.gridApi = gridApi;
        $scope.gridApi.core.on.rowsVisibleChanged($scope, function() {
            angular.forEach($scope.gridApi.selection.getSelectedGridRows(), function(row) {
              if (!row.visible) {
                  $scope.gridApi.selection.unSelectRow(row.entity)
                  };
            });

        });
      },
      columnDefs: [{name: 'FirstName'}, {name: 'LastName'}, {name: 'Job'}],
      data: [{"FirstName": "nir", "LastName": "OP", "Job": "Stack Overflow User"},
             {"FirstName": "Tim", "LastName": "Harker", "Job": "Stack Overflow User"},
             {"FirstName": "nir", "LastName": "OP", "Job": "Stack Overflow User"}]
    };
  }
]);
div[ui-grid] {
  height: 190px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.css" />
<div ng-app="app" ng-controller="MainCtrl">
  <div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
</div>

Upvotes: 2

Tim Harker
Tim Harker

Reputation: 2417

If only there was a gridOptions.removeSelectedAfterFilterd = true.

Here's an alternative that works:

var app = angular.module('app', ['ui.grid', 'ui.grid.selection']);
app.controller('MainCtrl', ['$scope', '$timeout',
  function($scope, $timeout) {
    $scope.gridOptions = {
      enableFiltering: true,
      enableSelectAll: true,
      enableRowSelection: true,
      enableRowHeaderSelection: false,
      enableFullRowSelection: true,
      showGridFooter: true,
      onRegisterApi: function(gridApi) {
        $scope.gridApi = gridApi;
        $scope.gridApi.core.on.filterChanged($scope, function() {
          $timeout(function() {
            angular.forEach($scope.gridApi.selection.getSelectedGridRows(), function(row) {
              if (!row.visible) $scope.gridApi.selection.unSelectRow(row.entity);
            });
          }, 10);
        });
      },
      columnDefs: [{name: 'FirstName'}, {name: 'LastName'}, {name: 'Job'}],
      data: [{"FirstName": "nir", "LastName": "OP", "Job": "Stack Overflow User"},
             {"FirstName": "Tim", "LastName": "Harker", "Job": "Stack Overflow User"},
             {"FirstName": "nir", "LastName": "OP", "Job": "Stack Overflow User"}]
    };
  }
]);
div[ui-grid] {
  height: 190px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/4.0.2/ui-grid.min.css" />
<div ng-app="app" ng-controller="MainCtrl">
  <div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
</div>

Let me know if you have any further questions, happy to help.

Upvotes: 1

Related Questions