How to filter my data? (ng-grid)
angularjs, filtering, javascript, ng-grid
Solution
I have found a way that updates instantly. Basically I hold a hidden set of all my data, and upon receiving new data or changing my filter - I apply this filter to the full data set and hand the grid the filtered version.
This lets me use comparators (i.e. age >= 50) in my filter, which is the purpose of this question.
// Full unfiltered data set
$scope.entries = []; // Updated and pushed to
$scope.gridOptions = {
// The grids already filtered data set
data: 'filteredEntries',
enableColumnResize: false,
multiSelect: false,
enableSorting: false,
selectedItems: $scope.selectedEntries,
}
$scope.$on("updateEntries", function(data) {
// My table is filled by socket pushes, this is where it is updated.
$scope.updateFilters();
}
$scope.$on("newFilter", function(newFilter) {
// This is where I update my filter
$scope.updateFilters();
}
$scope.updateFilters = function() {
// Filters the full set and hands the result to the grid.
$scope.filteredEntries = $filter('filter')($scope.entries, $scope.myFilter);
$scope.$digest();
}
// A modifiable limit, modify through newFilter so data is refiltered
$scope.lowerLimit = 50;
// My Filter
$scope.myFilter = function(entry) {
if (entry < $scope.lowerLimit) {
return false;
}
return true;
}
Problem
I think this is most likely very simple but I cannot find any clear documentation on how to add a filter outside of the 'filterText' that is shown on their website. What I am trying to do is something as simple as this: ``` $scope.filterOptions = { filter: $scope.myFilter, // <- How to do something like this? useExternalFilter: true } $scope.gridOptions = { data: 'entries', enableColumnResize: false, multiSelect: false, enableSorting: false, selectedItems: $scope.selectedEntries, filterOptions: $scope.filterOptions } $scope.lowerLimit = 50; // My Filter $scope.myFilter = function(entry) { if (entry < $scope.lowerLimit) { return false; } return true; } ``` Edit: Or maybe if I could filter the datasource somehow? I tried this: ``` $scope.gridOptions = { data: 'entries | filter: myFilter', enableColumnResize: false, multiSelect: false, enableSorting: false, selectedItems: $scope.selectedEntries, } ``` But it is throwing quite a few errors.