[英]Ng-grid filter with 2 condition
我需要根據過濾表
我正在使用ng-grid填充表。 它提供了基於單個列進行過濾的功能。
對於跨列比較,我使用grid.refresh()
函數。 此函數調用grid.registerRowsProcessor( $scope.columnComparionFilter, 200 )
函數。 我已經准備了columnComparionFilter
函數,該函數基於過濾器返回renderableRows
。 下面是示例代碼:
// 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;
};
兩個過濾器單獨都可以正常工作。 但是當我嘗試一起工作時。 默認情況下,它僅基於單個過濾器進行過濾。 它消除了跨列過濾器。
誰能幫我解決這個問題?
實際上grid.registerRowsProcessor()
是ng-grid
單列過濾器使用的核心功能。 因此,當我嘗試對跨過濾使用相同的功能時,這會導致同步問題,並且單列過濾器會影響我的自定義功能。
我使用了替代方法來修改ng-grid數據。 我已經使用notifyDataChange()
函數解決了。 內部外部過濾器函數columnComparionFilter
內部我正在基於過濾器准備數據子集,然后將gridOptions.data
更改為子集數據。 請參閱以下代碼:
// 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.