【问题标题】:How to filter my combined column? (ng-grid)如何过滤我的合并列? (ng-网格)
【发布时间】:2014-03-13 23:00:08
【问题描述】:

我有一个 ng-grid,其中一个列是合并列:

angular.forEach($scope.opportunitiesData,function(row){
            row.getABDisplayName = function(){
                return this.a + ' - ' + this.b;
            };

        });



$scope.gridOptions = {
            data: 'loadGridItems',
            headerRowHeight: 0,
            multiSelect: false,
            showFilter: false,
            columnDefs: [{field:'getABDisplayName()',displayName:"Tasks"},

                         ...],
            enableSorting: true,
            filterOptions: $scope.filterOptions
        };

我的问题是过滤器不适用于该字段。 我试图改变它,并将组合字段分解为 2 个字段,过滤器工作...... 不知道有什么区别,因为两个字段都是字符串。

【问题讨论】:

  • 通常更好的选择是拥有一个用于构建数据的函数/服务,您可以将它(数据)直接传递给 setPagingData(myBuildData,page,pageSize);
  • 数据是由工厂服务获取的……但我看不出它是如何相关的。
  • tomerz - 在下面查看我的答案,如果这对您有帮助,请告诉我。如果是这样,请投票作为答案。谢谢。

标签: angularjs filtering ng-grid


【解决方案1】:

我一直在看这个,因为我遇到了同样的问题(只有三个连接字段供列过滤)。最终我想出了以下几点:

您需要创建自己的过滤器,并完全绕过 ng-grid 的内置 filterOptions。

类似的东西:

// pass in your original dataset to filter, and filter text
app.filter('opportunitiesFilter',function(){
    return function(array, input){

    var match = [];                // will hold your newly filtered data
    if (input = '') return array;  // return orig data if nothing entered

    // use foreach to loop through your original array  
    angular.forEach(array, function(item){

        // your filter logic goes here - might be the following:
        if (item.indexOf(input) >= 0) match.push(item);
      });
    return match;
});

然后在您的控制器代码中,在您的搜索文本字段中添加一个监视,并在它更改时调用过滤器。您将原始数据数组和搜索字段文本传递给过滤器,并将生成的过滤数组绑定到您的 ng-grid 数据参数。确保在控制器签名中包含 $filter 调用。 IE。函数($scope, $http, $filter, ...){ }

// watch your input field for changes to filter on, call custom filter, and bind to grid
$scope.watch('searchText'), function(){
    $scope.'loadGridItems' = $filter('opportunitiesFilter')($scope.opportunitiesData, $scope.searchText);
}

这对我有用,尽管您可能需要根据需要调整过滤器 - 例如使用 .toLowerCase() 等进行不区分大小写的匹配等。

希望这对您有所帮助 - 我花了一些时间,但现在我可以走了。

干杯, 丹

【讨论】:

  • 感谢您的支持,但您认为这不是答案吗?如果您有更好的解决方案,请告诉我,或将其标记为答案。
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 2020-06-12
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多