Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is needed example with checkboxes #9

Closed
sandiktopas opened this issue Jul 15, 2014 · 5 comments
Closed

It is needed example with checkboxes #9

sandiktopas opened this issue Jul 15, 2014 · 5 comments

Comments

@sandiktopas
Copy link

Could someone give example with checkboxes?

@jhund
Copy link
Owner

jhund commented Jul 23, 2014

@sandiktopas it may take me a little bit to update the demo app, but in the meantime here are some pointers that should get you going:

I assume that you want to use a checkbox to filter your records on a boolean attribute. For this example, let's use is_expired. First you need to add the checkbox to your filter UI:

  <%= f.check_box :with_expired_only %> Expired only

Then you need to create a scope:

  scope :with_expired_only, lambda { |flag|
    return nil  if 0 == flag # checkbox unchecked
    where(is_expired: true)
  }

And make sure to add the new scope to your model's filterrific macro.

The detailed documentation is always a great resource to help you solve any remaining issues.

@mark100net
Copy link

Suppose you wanted checkboxes but were not filtering on a boolean attribute?

Suppose I had a books index and I wanted to supply a (dynamic) set of checkboxes to filter on authors (one checkbox per author so that I can get all books written by one of the checked authors).

This is somewhat similar to what is mentioned in the docs about using an array of country id's, but there is no view example.

If I have a scope with_author_ids that takes an array of id's as a parameter I can't apply that to each checkbox since it will result in only one value being passed with the form.

I got it to work by doing it without without the form_for's checkbox helper, i.e.:

<% Author.all.each do |a| %>
    <%= check_box_tag("with_author_ids[]", a.id, false, class: 'filterrific-periodically-observed') %><%= a.name %>
<% end %>

And then in the controller:

params[:filterrific][:with_buy_in_level] = params[:with_buy_in_level] unless params[:filterrific].nil?

This seems kind of cumbersome.

I wonder if anyone has a better suggestions but if not I hope at least this helps someone else trying to do this.

@steakchaser
Copy link

For Rails 4 how about using the collection_check_boxes helper:

f.collection_check_boxes :with_author_ids, Author.all.map{|a| a.name, a.id}, :last, :first

@steakchaser
Copy link

You can also do it this way with the check_box form helper:

<% Author.all.each do |a| %>
  <%= f.check_box(:with_author_ids, {multiple: true}, a.id, false %>
  <%= a.name %>
<% end %>

NOTE: I think you want to stick with using the form builder object f, without this filterrific cannot set defaults or remember choices via the session.

@jhund jhund closed this as completed May 4, 2015
@wtoalabi
Copy link

My need is slightly different. How do I make checkbox work with predefined values. For instance, I have a "hobby" field with values like Reading, Travelling, Dancing... I want users to select and filter as many hobbies as possible. How do I present this on the index view? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants