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

Custom filtering

Open thomasfl opened this issue 13 years ago • 23 comments

I can't find any examples on how to write custom filters for ng-grid. It's easy to write a custom filter that filters out rows from ng-grid by simply manipulating the data used by ng-grid, but I have a fairly large dataset and don't want to keep an extra copy of the data.

Is it possible to use the gridOptions.useExternalFilter option? I can't find any examples the explains what it does.

Here's a very simple plunk http://plnkr.co/edit/T6qaQX

thomasfl avatar Feb 18 '13 10:02 thomasfl

I think I got it. Trying to pipe my data through an angular filter:

$scope.gridOptions = {
    data: 'myData|gridFilter:filterOptions'
}

thomasfl avatar Feb 18 '13 12:02 thomasfl

And this is how to final plunker looks after updating http://embed.plnkr.co/T6qaQX

thomasfl avatar Feb 18 '13 13:02 thomasfl

I've been trying to figure out custom filtering, and your plunkers don't appear to have any filtering in them at all.

c0bra avatar Apr 02 '13 16:04 c0bra

Sorry, I've updated the plunk and have stopped working with angular since one month ago. If you do figure it out, please make a blog posts about it. There's a lack of good examples.

thomasfl avatar Apr 03 '13 13:04 thomasfl

I made a quick plunk here: http://plnkr.co/edit/raZXLH

It uses the built-in filterOptions.filterText property to do the filtering. One important thing to note is that if you change the displayName of the column in columnDefs, you MUST use that displayName as the column predicate in your filter text.

So if the displayName of the "name" column is set to "Dude Name", your custom filter text would be: "Dude Name:Nephi".

c0bra avatar Apr 03 '13 14:04 c0bra

Thanks for that plunk c0bra, helps a lot to understand the binding.

I am trying to implement a search box, just like the one that is built in (which is enabled by showFilter: true), but I want to be able to put it outside the grid, to customize the user interface. Do you know how to implement this?

horduringi avatar Jul 10 '13 17:07 horduringi

@horduringi like this? http://plnkr.co/edit/PHdBhF?p=preview

c0bra avatar Jul 26 '13 15:07 c0bra

This is exactly what I need. Thank you so much!

horduringi avatar Jul 30 '13 22:07 horduringi

Hey @c0bra , you should put your answer here: http://stackoverflow.com/questions/17977869/how-to-filter-my-data-ng-grid. I also found out that you can do a compound filter like this:

name:n;age:3

That would show the table:

Tiancum 43
Enos 34

jesslilly avatar Sep 20 '13 19:09 jesslilly

@jesslilly thanks for the tip; I've added an answer

c0bra avatar Sep 20 '13 20:09 c0bra

c0bra, great example.

for some reason when i tested the filtering i noticed that |, $, ^ is not taking into consideration as other special chars.

Is there any known reason for that?

ronenme avatar Sep 18 '14 14:09 ronenme

@ronenme I don't know off the top of my head. Filtering in 2.x can be all sorts of wonky and I wasn't involved in writing it. My personal first step would be to do a lot of googling, which you've probably already done.

c0bra avatar Sep 18 '14 14:09 c0bra

@c0bra , thanks for the response.

I will continue with the googling :+1:

ronenme avatar Sep 22 '14 08:09 ronenme

Nice example @c0bra , is there any chance I could do the same in 3.x?

rntorm avatar Oct 13 '14 15:10 rntorm

Refer #1765, short answer is that it is not currently possible in 3.x.

PaulL1 avatar Oct 13 '14 20:10 PaulL1

Thanks for the reference, seems that there is some kind of a workaround, will check it out tomorrow. However, is there a possibility that it will be included in a foreseeable future? @PaulL1

rntorm avatar Oct 13 '14 21:10 rntorm

It depends on how far into the future we can forsee. :-) I think having put the thought into #1765 I can see how it can be done, so the actual coding isn't a barrier. But I have quite a few things on my plate, and am potentially starting a new role soon that would reduce my time. So it's not likely that I'll personally do it in the near future. We're always open to new contributors though - and this is something I think someone new to the team could achieve reasonable easily. Check the first timers guide https://github.com/angular-ui/ng-grid/blob/master/FIRST_TIMER.md

PaulL1 avatar Oct 13 '14 21:10 PaulL1

I looked at the workaround at #1765 and also the ui-grid source code. It seems that there could be a simpler solution than in #1765 that would allow to reuse the existing filter code. By passing giving external ng-model as colDef value( e.g. externalFilter) which could then be set as the "filter.term". Unfortunately I failed to grasp where is $watch being set for the internal filter inputs. Are you familiar with this side of the code and maybe could point me to the right direction @PaulL1 ?

rntorm avatar Oct 14 '14 16:10 rntorm

You could do this if you're happy for the filter to run against only a single column. The filter logic in 3.0 is column by column.

PaulL1 avatar Oct 14 '14 19:10 PaulL1

Is there way to filter {blank} data? I've tried using null, empty but wasn't successful.

sparkinnovations avatar Oct 21 '14 14:10 sparkinnovations