How to filter my data? (ng-grid)

后端 未结 3 917
夕颜
夕颜 2020-12-23 14:31

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

3条回答
  •  遥遥无期
    2020-12-23 15:28

    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;
     }
    

提交回复
热议问题