我在我的rails应用中使用 filterrific gem来过滤结果,我根据选项过滤了事件 我的代码如下
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>
所以我在这里做的是我用一个选择框根据他们的类别过滤事件 但现在我想在选择框中添加选项框必须包含复选框元素的功能,以便我可以过滤多个类别结果,这样当我检查一个类别时,它将按该单个类别过滤结果,当我点击另一个类别,它将过滤该类别和之前的类别
的结果提前解决这个问题很有帮助
答案 0 :(得分:0)
您可以使用multiple: true
,如下所示:
<%= f.select(
:with_category_id,
@filterrific.select_options[:with_category_id],
{ include_blank: ' Any ' },
{ multiple: true, class: 'select-filter'}
) %>