Ryan Langton
Ryan Langton

Reputation: 6160

ng-grid get filtered column count after filtering

I'm using ng-grid with filtering. Any time the filter updates I want to get the filtered item count. I have been able to do this using the filteredRows property of ngGrid. However I'm getting the rows BEFORE the filtering occurs and I want them AFTER the filtering occurs. Here is a plunker to demonstrate the behavior: http://plnkr.co/edit/onyE9e?p=preview

Here is the code where filtering is occuring:

$scope.$watch('gridOptions.filterOptions.filterText2', function(searchText, oldsearchText) {
      if (searchText !== oldsearchText) {
        $scope.gridOptions.filterOptions.filterText = "name:" + searchText + "; ";
        $scope.recordCount = $scope.gridOptions.ngGrid.filteredRows.length;
    }
});

Upvotes: 2

Views: 2319

Answers (1)

mainguy
mainguy

Reputation: 8331

After looking at the source and some debugging I think that the actual filtering of the grid happens after your watcher fires. Same goes for listening to ngGridEventFilter which also fires before the rows are filtered.

The solution I came up with is listening to ngGridEventRows:

    $scope.$watch('gridOptions.filterOptions.filterText2', function(searchText, oldsearchText) {
    if (searchText !== oldsearchText) {
      $scope.gridOptions.filterOptions.filterText = "name:" + searchText + "; ";
    }
    });
    $scope.$on('ngGridEventRows', function() {
    $scope.recordCount = $scope.gridOptions.ngGrid.filteredRows.length;
    });

This seems to work in this Plunker

From my recent experiments with gridevents I learned that the row event is fired very often (when you scroll for example for every row), so I am not sure if this is really the best practice. But since you are updating only one lousy counter it does not have a huge impact on performance.

Still someone else may come up with a better idea. Please keep me informed how this was finally solved.

Upvotes: 2

Related Questions