如何过滤我的数据?(ng-grid) [英] How to filter my data? (ng-grid)
问题描述
我认为这很可能非常简单,但我找不到任何关于如何在其网站上显示的filterText"之外添加过滤器的明确文档.我想要做的就是这么简单:
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;
}
或者我是否可以以某种方式过滤数据源?我试过这个:
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.
推荐答案
我找到了一种即时更新的方法.基本上,我持有我所有数据的隐藏集,并在收到新数据或更改我的过滤器时 - 我将此过滤器应用于完整数据集并将过滤版本交给网格.
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.
这让我可以在过滤器中使用 comparators(即年龄 >= 50),这就是这个问题的目的.
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;
}
这篇关于如何过滤我的数据?(ng-grid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!