2012-11-06 44 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的选择输入。