简体   繁体   English

带有2个条件的Ng网格过滤器

[英]Ng-grid filter with 2 condition

I have requirement to filter table based on 我需要根据过滤表

  1. single column and 单列和
  2. across column comparison. 跨列比较。

I am using ng-grid to populate table. 我正在使用ng-grid填充表。 It gives feature to filter based on single column. 它提供了基于单个列进行过滤的功能。

For across column comparison I am using grid.refresh() function. 对于跨列比较,我使用grid.refresh()函数。 this function calls grid.registerRowsProcessor( $scope.columnComparionFilter, 200 ) function. 此函数调用grid.registerRowsProcessor( $scope.columnComparionFilter, 200 )函数。 I have prepared columnComparionFilter function that returns renderableRows based on filter. 我已经准备了columnComparionFilter函数,该函数基于过滤器返回renderableRows Below is sample code: 下面是示例代码:

// function to filter two column based on condition
$scope.columnComparionFilter= function(renderableRows){
    // if filter option selection changed
    if($scope.change){
        if($scope.column1 == undefined)
            return renderableRows;
        if($scope.column2 == undefined)
            return renderableRows;
        if($scope.column1 == $scope.column2){
            console.log('columns for comparision should be different.')
            return renderableRows;
        }
        if($scope.selectedFilter.name == 'compare'){
            renderableRows.forEach( function( row ) {
                if(row.entity[$scope.column1.field] == row.entity[$scope.column2.field])
                    row.visible = true;
                else
                    row.visible = false;
            });
        }else if($scope.selectedFilter.name == 'distinct'){
            renderableRows.forEach( function( row ) {
                if(row.entity[$scope.column1.field] != row.entity[$scope.column2.field])
                    row.visible = true;
                else
                    row.visible = false;
            });
        }
    }
    return renderableRows;
};

Both filter works fine alone. 两个过滤器单独都可以正常工作。 But when I tries working both together. 但是当我尝试一起工作时。 It filters based on by default single filter only. 默认情况下,它仅基于单个过滤器进行过滤。 It eliminates across column filter. 它消除了跨列过滤器。

Can any one help me to solve this? 谁能帮我解决这个问题?

Actually grid.registerRowsProcessor() is core function used by single column filter of ng-grid . 实际上grid.registerRowsProcessor()ng-grid单列过滤器使用的核心功能。 So when I tried to use same function for across filtering, It lead synchronization problem and single column filter over ruled my customised function. 因此,当我尝试对跨过滤使用相同的功能时,这会导致同步问题,并且单列过滤器会影响我的自定义功能。

I have used an alternative for modifying ng-grid data. 我使用了替代方法来修改ng-grid数据。 I have solved it using notifyDataChange() function. 我已经使用notifyDataChange()函数解决了。 inside external filter function columnComparionFilter I am preparing subset of data based on filter and then I am changing gridOptions.data to subset data. 内部外部过滤器函数columnComparionFilter内部我正在基于过滤器准备数据子集,然后将gridOptions.data更改为子集数据。 See below code: 请参阅以下代码:

// function to filter two column based on condition
$scope.columnComparionFilter= function(renderableRows){
    // if filter option selection changed
    if($scope.change){
        if($scope.column1 == undefined){
            console.log('Please select first column to compare.')
            return;
        }
        if($scope.column2 == undefined){
            console.log('Please select second column to compare.')
            return;
        }
        if($scope.column1 == $scope.column2){
            console.log('columns for comparision should be different.')
            return;
        }
        if($scope.selectedFilter.name == 'compare'){
            renderableRows.forEach( function( row ) {                
                if(row[$scope.column1.field] == row[$scope.column2.field])
                    filterData.push(row)
            });
        }else if($scope.selectedFilter.name == 'distinct'){
            renderableRows.forEach( function( row ) {
                if(row[$scope.column1.field] != row[$scope.column2.field])
                    filterData.push(row)
            });
        }
        $scope.gridOptions.data = angular.copy(filterData);
        $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
        return;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM