1

我正在通过使用 selected 和 ancestry gem 来寻找动态选择,我希望以前有人这样做过。

我有大约 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
4

1 回答 1

1

我使用 select2 但我认为也可以使用 selected 。所以,你必须使用ajax请求。在服务器端定义一个动作,获取类别 id(所选类别的 id),并返回所选类别 id 的子类别的集合(以 json 格式)。然后在客户端(使用 jquery 或 javasciprt),您必须使用 json 对象填充选择输入。

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

填充选择输入:jQuery-- 从 json 填充选择

在 ActiveAdmin 中,您必须根据 DOM 确定选择输入。

于 2014-05-30T20:03:13.610 回答