2012-11-06 46 views
1

我正在尋找動態選擇通過使用選擇和祖先的寶石,我希望有人做過之前。動態選擇祖先的寶石,並選擇

我有2000年左右(類別,子類別和關鍵字)都在STI和樹形結構。在我的表單中,我爲類別|使用了三個選擇輸入子類別|關鍵字,但我得到了巨大的子類別列表,甚至更大的關鍵字列表。我想隱藏所有子類別,它們不是預選類別的子類;與關鍵字一樣。

希望這是有道理的,我正在嘗試做什麼。我非常感謝任何想法。

這裏我到目前爲止,所有這些代碼工作很好。

class Company < ActiveRecord::Base 
has_and_belongs_to_many :categories, :join_table => "companies_categories" 
has_and_belongs_to_many :subcategories, :join_table => "companies_subcategories" 
has_and_belongs_to_many :keywords, :join_table => "companies_keywords" 
end 

class Category < ActiveRecord::Base 
has_ancestry :cache_depth => true, :depth_cache_column => :ancestry_depth 

has_many :subcategories, :class_name => "Category", :foreign_key => :subcategory_id 
has_many :keywords, :class_name => "Category", :foreign_key => :keyword_id 

belongs_to :category 

has_and_belongs_to_many :companies, :join_table => "companies_categories" 
has_and_belongs_to_many :companies, :join_table => "companies_subcategories", :association_foreign_key => "subcategory_id" 
has_and_belongs_to_many :companies, :join_table => "companies_keywords", :association_foreign_key => "keyword_id" 

end 

這裏是JavaScript的

jQuery(function($){ 
$(".chosen-input").chosen(); 
$(".schosen-input").chosen(); 
$(".kchosen-input").chosen(); 
});  

這裏是我的形式

form :html => { :enctype => "multipart/form-data" } do |f| 
f.inputs "New Company" do 
    f.input :name, :required => true 
end 

f.inputs "Categories" do 

    f.input :categories, 
      :input_html => { :class => "chosen-input", :multiple => true, :style => "width: 700px;"}, 
      :collection => Category.where(:ancestry_depth => '2') 


    f.input :subcategories, 
      :input_html => { :class => "schosen-input", :multiple => true, :style => "width: 700px;"}, 
      :collection => Category.where(:ancestry_depth => '3') 


    f.input :keywords, 
      :input_html => { :class => "schosen-input", :multiple => true, :style => "width: 700px;"}, 
      :collection => Category.where(:ancestry_depth => [4, 5, 6]) 

end 
+0

我也有興趣...也許這可以幫助你? http://staal.io/blog/2013/02/26/mastering-activeadmin/ – James

回答

1

我用選擇2,但我認爲這是可能的選擇,太多。所以,你必須使用ajax請求。 在服務器端定義一個操作,得到一個類別id(所選類別的id),並返回所選類別id的子類的集合(以json格式)。然後在客戶端(使用jquery或javasciprt),您必須使用json對象填充選擇輸入。

JQuery的事件中,你可以使用:http://api.jquery.com/change/

填充選擇輸入:jQuery-- Populate select from json

而且在ActiveAdmin你要確定基於DOM的選擇輸入。