设置filterrific是一件轻而易举的事情,它完美无缺,但现在我们想要做一些更复杂的事情,而我似乎无法弄清楚如何最好地进行:
在管理更新的应用程序中,我们有系统和包。每个包has_many包版本和每个包版本可以与系统关联(通过ConcretePackageVersion多对多)。
现在我们想要一个带有两个 filterrific表单的视图,一个用于System,另一个用于Package,用户可以在这两种方式中过滤,并且显示包,它与已过滤的系统有连接,并且也会传递包过滤器。
我的想法是使用Packages进行通常的filterrific设置,然后为系统添加另一个filterrific表单,然后影响Packages'过滤器。问题和我的问题是: 第二种表单/过滤器应如何影响第一种表单?
版本:
$ gem list | grep filter
filterrific (2.0.5)
到目前为止,这是我的代码:
CombinationsController
def index
@filterrific = initialize_filterrific(
Package,
params[:filterrific],
:select_options => {
sorted_by: Package.options_for_sorted_by,
with_package_group_id: PackageGroup.options_for_select
}
) or return
@packages = @filterrific.find.page(params[:page])
@filterrific_systems = initialize_filterrific(
System,
params[:filterrific],
:select_options => {
sorted_by: System.options_for_sorted_by,
with_system_group_id: SystemGroup.options_for_select
}
) or return
@systems = @filterrific_systems.find.page(params[:page])
end
视图/组合/ index.html.erb
<%= form_for_filterrific @filterrific_systems do |f| %>
<%= f.text_field( :sys_search_query, class: 'filterrific-periodically-observed') %>
# more fields
<%= render_filterrific_spinner %>
<% end %>
<%= form_for_filterrific @filterrific do |f| %>
<%= f.text_field( :search_query, class: 'filterrific-periodically-observed') %>
# more fields
<%= render_filterrific_spinner %>
<% end %>
<div id="filterrific_table">
<%= render(
partial: 'combinations/list',
locals: {
packages: @packages,
systems: @systems
}
) %>
</div>
视图/组合/ index.js.erb的
<% js = escape_javascript(
render(partial: 'combinations/list', locals: {
packages: @packages,
systems: @systems
})
) %>
$("#filterrific_table").html("<%= js %>");
模型/ system.rb
filterrific(
default_filter_params: { sorted_by: 'registered_at_desc' },
available_filters: [
:sorted_by,
:sys_search_query,
:with_system_group_id
]
)
# scopes are pretty much the same as in the demo application
模型/ package.rb
filterrific(
default_filter_params: { sorted_by: 'name_desc' },
available_filters: [
:sorted_by,
:search_query,
:with_package_group_id
]
)
#more scopes here as well