ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

How to setup a custom filter function

Open jaydeepk opened this issue 12 years ago • 10 comments

Hi

I am trying to implement a custom filter function for a ng grid but it doesn't seem to work.

What I want is some way to pipe the data binded to the grid via a filter function.

This is mainly to avoid keeping a duplicate copy of the entire data on the controller.

This is how I ideally want it to work :

$scope.gridOptions = { data: 'clientsData|gridFilter:filterFunction',
filterOptions: $scope.filterOptions,

$scope.filterFunction = function (elem) { // Filtering logic

};

$scope.filterOptions = { filterText: '', useExternalFilter: true };

It seems that something similar has been attempted : https://github.com/angular-ui/ng-grid/issues/165

But I'm unable to get it working ... any ideas ?

Thanks

jaydeepk avatar Jan 03 '14 05:01 jaydeepk

I think ngGrid should have an easier way to define filtering function that accepts an element and filtering text. The default implementation of ngGrid filtering is quite slow when there are lots of rows (~100k) so I need to implement my custom filtering as follows. I hope we would have a filterFn parameter in input options.

var grid = $scope.gridOptions.ngGrid;
var $gridScope = $scope.gridOptions.$gridScope;

var filterFn = function (item, filterText) {
    filterText = filterText.toLocaleLowerCase();
    return (item.name.toLocaleLowerCase().indexOf(filterText) >= 0);
};

$gridScope.$watch('filterText', function(a){
    if (a) {
        grid.filteredRows = [];
        for(var i = 0; i < grid.rowCache.length; i++) {
            if (filterFn(grid.rowCache[i].entity, a)) {
                grid.filteredRows.push(grid.rowCache[i]);
            }
        }
    }else{
        grid.filteredRows = grid.rowCache;
    }
    grid.rowFactory.filteredRowsChanged();
});

baonhan avatar Jan 03 '14 06:01 baonhan

Thanks a lot baonhan. I am trying to get it working as per the code that you have shared. However I always get $scope.gridOptions.ngGrid and $scope.gridOptions.$gridScope as 'undefined'. Any idea if I'm missing something ? Btw .. we are using ngGrid 2.0.7 at the moment.

jaydeepk avatar Jan 03 '14 10:01 jaydeepk

Those are initialized after the grid is ready. You can use 'init' in options to get the callback:

var gridInit = function(grid, $gridScope){
     .....
}
$scope.gridOptions = {
     ........
     init: gridInit
}

baonhan avatar Jan 03 '14 10:01 baonhan

Brilliant ..works for me now...Thanks a lot once again baonhnan. :) Btw ..just curious ..the init property does not seem to be documented anywhere ..how did you find it ?

jaydeepk avatar Jan 03 '14 10:01 jaydeepk

Oh I see it in the code so I thought it had been documented: https://github.com/angular-ui/ng-grid/blob/master/src/directives/ng-grid.js#L163

baonhan avatar Jan 03 '14 16:01 baonhan

Okay .. cool ..thanks.

jaydeepk avatar Jan 04 '14 08:01 jaydeepk

@baonhan Can you please provide me proper plunker for the same and the link which you have mentioned( https://github.com/angular-ui/ng-grid/blob/master/src/directives/ng-grid.js#L163) is also not working.

Seek help !! Avid for you reply.

tmpathak-cygnet avatar Dec 31 '15 07:12 tmpathak-cygnet

I've got below solution to have 4 different types of filters in UI Grid. you can add more conditions to add more types/falvours.

filter:{ condition:function(searchTerm, cellValue){ if(searchTerm.startsWith("%") && searchTerm.endsWith("%")){ //contains searchTerm = searchTerm.replace(new RegExp("%","g"),""); return new RegExp(searchTerm, "i").test(cellValue); }else if(searchTerm.startsWith("%")){ //ends with searchTerm = searchTerm.replace(new RegExp("%","g"),""); return new RegExp(searchTerm + "$", "i").test(cellValue); } else if(searchTerm.endsWith("%")){ //starts with searchTerm = searchTerm.replace(new RegExp("%","g"),""); return new RegExp("^" + searchTerm, "i").test(cellValue); }else{ //exact return new RegExp("^" + searchTerm + "$", "i").test(cellValue); } }

rashmingadhvi avatar Nov 21 '17 16:11 rashmingadhvi

Simplified version -

filter:{ condition:function(searchTerm, cellValue){ var x = $(filter)('yourcustomfilter')(a,b); if(x.length) return true; // if you want to show the cell value } }

sourabh504 avatar Jan 18 '18 13:01 sourabh504

condition: function is not invoked when I type a special character like steric "*". Anyone faced this issue?

umairnow avatar Sep 13 '18 10:09 umairnow