filterrific icon indicating copy to clipboard operation
filterrific copied to clipboard

Check if filters are set in the view

Open octant opened this issue 9 years ago • 12 comments

I was wondering if there's a way to check if the filters are set in the view. I'd like to provide a hint to my users that the view is currently filtered.

octant avatar Feb 26 '15 14:02 octant

@octant great suggestion. I have thought about this in the past but never got around to implementing it. I think it's possible and I can add this to filterrific pretty quickly. What do you think of the following API:

We want a flag that indicates if Filterrific's default settings have been modified by either adding another filter, or changing one of the default values.

This would be a method on the @filterrific object. What should we call this method?

  • #default_settings?
  • #has_filters_applied?

Hmm. Do you have any suggestions?

Thanks for using Filterrific.

jhund avatar Feb 26 '15 18:02 jhund

For this particular use case I think #has_filters_applied? reads better than #default_settings?.

Thank you for creating such a great gem!

octant avatar Feb 26 '15 19:02 octant

@octant sorry for the delay on this. I was traveling last week and am kind of stuck on what to do if a filterrific default param is given as a Proc. I'm still working on this, just need a bit more time.

jhund avatar Mar 10 '15 19:03 jhund

To expand on this, I would like my users to see what filters are in place and to be able to selectively remove filters. Something like this:

http://www.qualtrics.com/wp-content/uploads/2013/08/delete.png http://i.stack.imgur.com/BKKvw.png

Would it make sense to also have a #applied_filters method that returns an array/hash of filters in place?

alankessler avatar Mar 17 '15 00:03 alankessler

@alankessler great idea on the #applied_filters method. I'm thinking of implementing 'dirty' object tracking on the @filterrific object. If @filterrific is initialized with the defaults, it's considered clean. If it's initialized with params that are different from the defaults, it's considered 'dirty'.

This approach will allow me to return a collection of custom filter params that can be used to clear filters as shown in your screenshots.

I'm thinking of these two new methods:

  • #custom_filter_params, returns an Array of Hashes like so: [{ name: :search_query, value: 'foo' }]
  • #has_custom_filter_params, implemented as custom_filter_params.any?, returning true or false

jhund avatar Mar 18 '15 05:03 jhund

That would do it. Seems like a reasonable solution.

alankessler avatar Apr 09 '15 06:04 alankessler

jhund, did you happen to make any progress on this?

alankessler avatar Jun 22 '15 18:06 alankessler

@alankessler unfortunately I haven't resolved this yet. I could use it in one of my other open source projects. So the motivation is there, it's just not super high on my priority list right now.

jhund avatar Jun 22 '15 18:06 jhund

@alankessler I started working on this, and then got stuck on how to handle Procs as default values. I'm sure I can figure it out, just need to sit down and do it...

jhund avatar Jun 22 '15 18:06 jhund

Just noticed the 2.0.5 version already contains a stub for this feature :)

# Returns true if this instance of Filterrific has params other than the
# defaults.
# @return [Boolean]
def has_filters_applied?
  sdf
end

Would be great if this was completed :+1:

tamaloa avatar Apr 06 '16 12:04 tamaloa

@jhund Suggested implementation & temporary monkey patch.

Filterrific::ParamSet.class_eval do

  def has_filters_applied?
    self.to_hash.keys.length > 0
  end

end

joshuateveryday avatar Oct 06 '16 02:10 joshuateveryday

Have there been any progress on this?

alydersen avatar Nov 07 '19 09:11 alydersen