[英]Active Admin: How to customize labels for select filter?
這似乎應該相當簡單,買到我還沒有找到關於這個主題的任何文件。
我有以下過濾器:
filter :archived, as: :select
...它給了我一個選擇框形式的工作過濾器,選項“Any”,“Yes”和“No”。
我的問題是:如何自定義這些標簽,使功能保持不變,但標簽是“全部”,“實時”和“已存檔”?
快速簡便:
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 / inputs(如建議formtastic)我創建兩個clases:
在app / inputs / 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 / inputs / filter_country_select_input.rb中:
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
在我的app / admin / 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在不同的上下文,索引過濾器或表單輸入中查找Filter [:your_custom_name:] Input和[: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
它不是“干凈”也不是“優雅”,但效果還不錯:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.