nir nir -4 years ago 190
AngularJS Question

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

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download