简体   繁体   中英

Ruby on rails 4 using filterrific to filter based on checkbox

I am using filterrific gem in my rails app to filter the result, i have events to be filtered based on the options my code is as follows

event.rb

filterrific :default_filter_params => { :sorted_by => 'created_at_desc' },
              :available_filters => %w[
                sorted_by
                search_query
                with_category_id
                with_area
              ]

 # default for will_paginate
   self.per_page = 9

scope :with_category_id, lambda { |category_ids|
                          where(category_id: [*category_ids])
                        }

events_controller.rb

def index
  @filterrific = initialize_filterrific(
        Event,
        params[:filterrific],
        :select_options => {
             sorted_by: Event.options_for_sorted_by,
             with_category_id: Category.options_for_select

        }
    ) or return
    @events = @filterrific.find.paginate(page: params[:page], per_page: 6).where(is_job: false)


  respond_to do |format|
      format.html
      format.js
    end
end

index.html.erb

<%= form_for_filterrific @filterrific, :class => 'col s12' do |f| %>

          <%= render_filterrific_spinner %>
          <%= f.hidden_field :source , :value => 'event' %>
          <div class="col s12 m3 ">
            <div class="filter-container">
             <li>
                  <%= f.select(
                                  :with_category_id,
                                  @filterrific.select_options[:with_category_id],
                                  { include_blank: ' Any ' },
                                  class: 'select-filter'
                          ) %>

                      <label>select category</label>

                </li>
             </div>
            <%= link_to(
                        'Reset filters',
                        reset_filterrific_url,
                ) %>
          </div>
      <% end %>

<%= render(
                                                 partial: 'events/list',

                                         ) %>

index.js.erb

<% js = escape_javascript(
  render(partial: 'events/list')
) %>
$("#filterrific_results").html("<%= js %>");
$('#filterrific_results').load(document.URL +  ' #filterrific_results');

_list.html.erb

<div id="filterrific_results">
<% @events.each do |event| %>
<%= link_to event.name, company_event_path(event.company.id, event.id) %>
 <% end %>

</div>

so what i am doing here is i filtered the events based on their category with a select box but now i want to add the feature in the select box that the select box must contain the checkbox elements so that i can filter multiple category result, so that when i check on one category it will filter the result by that single category and when i click another category it will filter the result with that category and previous category checked

Would be a great help to fix this , Thankx in advance

you can use multiple: true , like this:

<%= f.select(
                                  :with_category_id,
                                  @filterrific.select_options[:with_category_id],
                                  { include_blank: ' Any ' },
                                  { multiple: true, class: 'select-filter'}
                          ) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM