-
Notifications
You must be signed in to change notification settings - Fork 0
Filters
Lukas Svoboda edited this page Jan 27, 2014
·
10 revisions
Basic filter's configuration options:
- filters - by default for all visible fields
- searchable - used for searching and filters.
- queryable [true/false]
- filterable [true/false]
See List (section Fields searching) for details. Example:
rails_admin do
query do
include_all_fields
visible_fields.each do |f|
f.queryable false
end
field :full_name do
searchable [:b_title, :first, :surname, :a_title]
queryable true
filterable false
end
field :selected do
queryable true
end
end
end
end
To add custom filter to the filter's dropdown in the filter dialog, you have to:
- Define a special named scope. It has to start on
filter_
prefix and use 3 arguments
def self.filter_tag_list(scope, value, auth_object)
scope.tagged_with(value)
end
def self.filter_magazine_issue_when(scope, values, auth_object)
scope.where("1 = ? and 2 = ? and 3 = ?", values[:magazine], values[:issue], values[:when])
end
after that the filter can recognize f['tag_list_eq'] and f['magazine_issue_when_eq'] parameters.
- Configure
_query
partial to render a new menu item and its form. Following example configure two new items "Tags" and "Complex Example". File:app/views/customers/_query.html.haml
- query_form.configure do |form|
- form.default_group.field :tag_list, filterable: true, label: "Tags" do |field|
- field.content do
%input{class: "input-large", type: "text", name: "<%- name %>".html_safe, value: "<%- value || '' %>".html_safe,
data: { filteringselect: true, options: {placeholder: I18n.t('admin.misc.search'), remote_source: tag_list_customers_path}.to_json }}
- form.default_group.field :magazine_issue_when, filterable: true, label: "Test" do |field|
- field.content do
.form-inputs
%input{class: "input-medium", type: "text", name: "<%- name %>[magazine]".html_safe, value: "<%- value.magazine || '' %>".html_safe,
data: { filteringselect: true, options: {placeholder: I18n.t('admin.misc.search'), remote_source: options_magazines_path}.to_json }}
- param = "f[magazine_id_eq]"
-# TODO - this is ugly (just because to keep on the same place) -> better to ejs template or render method in query_form
%input{class: "input-medium", type: "text", name: "<%- name %>[issue]".html_safe, value: "<%- value.issue || '' %>".html_safe,
data: { filteringselect: true,
options: %Q{{"placeholder":"#{I18n.t('admin.misc.search')}","remote_source":"#{options_issues_path}","remote_source_params":{"#{param}":"<%= value.magazine %>"}}}.html_safe,
"dependant-filteringselect" => "field=magazine", "dependant-param" => param }}
:plain
<%= select_option(name + '[when]', value.when, [['0', 'libovolny'], ['1', 'prvni'], ['2', 'druhy'], ['3', 'posledni'], ['4', 'predposleni']]) %>
= form_render query_form
For defining complex scope condition on query, you can define class method default_query(term)
on the model.
Example:
def self.default_query(term)
return [] if term.to_s.blank?
query = Payment.all
terms = term.split(',')
terms.each do |t|
t.strip!
t_cont = "%#{t}%"
query = query.
where("(vs like ?) OR (ss like ?) OR (note like ?) OR (bank_note like ?) or (from_account like ?) OR (CAST(price AS text) = ?)",
t_cont, t_cont, t_cont, t_cont, t_cont, t)
end
query.order('updated_at')
end