这似乎应该是相当简单的,买我一直没有找到任何有关这个问题的文件。活动管理员:如何为选择过滤器自定义标签?
我有以下过滤器:
filter :archived, as: :select
...这使我的选择“任意”,“是”和“否”的选择框的形式工作的过滤器。
我的问题是:如何定制这些标签,使其功能保持不变,但标签是“全部”,“实时”和“存档”?
这似乎应该是相当简单的,买我一直没有找到任何有关这个问题的文件。活动管理员:如何为选择过滤器自定义标签?
我有以下过滤器:
filter :archived, as: :select
...这使我的选择“任意”,“是”和“否”的选择框的形式工作的过滤器。
我的问题是:如何定制这些标签,使其功能保持不变,但标签是“全部”,“实时”和“存档”?
简单快捷:
filter :archived, as: :select, collection: [['Live', 'true'], ['Archived', 'false']]
不过,这不会给你一个方法来定制不改变的I18n“全部”选项。
更新:这里是另一种选择:
# Somewhere, in an initializer or just straight in your activeadmin file:
class ActiveAdmin::Inputs::FilterIsArchivedInput < ActiveAdmin::Inputs::FilterSelectInput
def input_options
super.merge include_blank: 'All'
end
def collection
[ ['Live', 'true'], ['Archived', 'false'] ]
end
end
# In activeadmin
filter :archived, as: :is_archived
我有同样的问题,但我需要在索引过滤器和表单输入自定义选择,让我发现了一个类似的解决方案: 在app /输入(如建议formtastic)我创建两个clases:
在app /输入/ country_select_input.rb:
class CountrySelectInput < Formtastic::Inputs::SelectInput
def collection
I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code|
translation = I18n.t(country_code, scope: :countries, default: 'missing')
translation == 'missing' ? nil : [translation, country_code]
}.compact.sort
end
end
在app /输入/ filter_country_select_input.r B:
class FilterCountrySelectInput < ActiveAdmin::Inputs::FilterSelectInput
def collection
I18nCountrySelect::Countries::COUNTRY_CODES.map { |country_code|
translation = I18n.t(country_code, scope: :countries, default: 'missing')
translation == 'missing' ? nil : [translation, country_code]
}.compact.sort
end
end
而且在我的应用程序/管理/ city.rb:
ActiveAdmin.register City do
index do
column :name
column :country_code, sortable: :country_code do |city|
I18n.t(city.country_code, scope: :countries)
end
column :created_at
column :updated_at
default_actions
end
filter :name
filter :country_code, as: :country_select
filter :created_at
form do |f|
f.inputs do
f.input :name
f.input :country_code, as: :country_select
end
f.actions
end
end
正如你所看到的,ActiveAdmin查找筛选[:your_custom_name:]输入和[:your_custom_name:]中输入不同的上下文,索引过滤器或表单输入。所以,你可以创建这个扩展的ActiveAdmin :: Inputs :: FilterSelectInput或Formtastic :: Inputs :: SelectInput并定制你的逻辑。
它的工作对我来说,我希望你能发现它有用
这里有一个工作......而不需要编写新的输入控件一劈!
filter :archived, as: :select, collection: -> { [['Yes', 'true'], ['No', 'false']] }
index do
script do
"""
$(function() {
$('select[name=\"q[archived]\"] option[value=\"\"]').text('All');
});
""".html_safe
end
column :id
column :something
end
这不 “干净”,也不是 “优雅”,但效果还不错:)