2013-04-06 34 views
7

這似乎應該是相當簡單的,買我一直沒有找到任何有關這個問題的文件。活動管理員:如何爲選擇過濾器自定義標籤?

我有以下過濾器:

filter :archived, as: :select 

...這使我的選擇「任意」,「是」和「否」的選擇框的形式工作的過濾器。

我的問題是:如何定製這些標籤,使其功能保持不變,但標籤是「全部」,「實時」和「存檔」?

回答

13

簡單快捷:

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 
0

我有同樣的問題,但我需要在索引過濾器和表單輸入自定義選擇,讓我發現了一個類似的解決方案: 在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並定製你的邏輯。

它的工作對我來說,我希望你能發現它有用

0

這裏有一個工作......而不需要編寫新的輸入控件一劈!

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 

這不 「乾淨」,也不是 「優雅」,但效果還不錯:)

相關問題