Filters based on a function in the condition are broken in versions after 3.1.1
Using a function as a filter condition no longer works after version 3.1.1.
This is the code from the grid options that fails:
{ field: 'gender', filter: {
noTerm: true,
type: uiGridConstants.filter.SELECT,
selectOptions: [ { value: '1', label: 'male' }, { value: '2', label: 'female' }, { value: '3', label: 'unknown'}, { value: '4', label: 'not stated' }, { value: '5', label: 'a really long value that extends things' } ],
condition: function (searchTerm, cellValue) {
if (searchTerm === undefined) {
if (cellValue === '1') {
return $scope.showMen;
} else {
return true;
}
} else {
return (searchTerm === cellValue);
}
}
},
cellFilter: 'mapGender', headerCellClass: $scope.highlightFilteredHeader },
As an example here are two plunkrs:
- On page load the Gender column should be restricted to NOT show any "male" rows.
- Checking or un-checking the "Show Men" box should cause them to appear or be hidden.
This one works with ui-grid 3.1.1: http://plnkr.co/edit/fgeIIVwwEwrz3g7nXwbH?p=preview
This one does NOT work with ui-grid 3.2.2: http://plnkr.co/edit/wW5ImICHq9muF5jVsSdx?p=preview
The only difference between the two is the loaded version of ui-grid in the index.html.
Wouldn't it make more sense to achieve this by registering a custom rows processor?
gridApi.core.registerRowsProcessor(function (rows) {
var col = gridApi.grid.getColumn('gender');
rows.forEach(function (row) {
if (row.entity.gender === "1" && !$scope.showMen && col.filter.term != "1") {
row.visible = false;
}
});
return rows;
}, 99);
I've added the col.filter.term != "1" bit because even though I've set the priority of the custom row processor to 99, and filtering has a priority of 100, male rows do not show with showMales=false and filter dropdown set to male, as is the case in your working example. This seems like a bug to me. The filter should be evaluating AFTER the custom processor, so it should override.
With col.filter.term != "1":
http://plnkr.co/edit/8ZrTodl0C9RCoJhb7Yst?p=preview
Without: http://plnkr.co/edit/yg5XwFKf1Z5Ra4vUSo5g?p=preview
Thanks to Atmos206 suggestion I have been able to bypass this issue. I will assume that I was just doing it wrong to begin with. I've been using the latest version in production with the above syntax for over a week now and all seems fine, so I will close this issue.