【问题标题】:Dynamic select with ancestry gem and chosen带有祖先宝石的动态选择并被选中
【发布时间】:2012-11-06 04:19:34
【问题描述】:

我正在通过使用 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

【问题讨论】:

标签: ruby-on-rails activeadmin jquery-chosen ancestry


【解决方案1】:

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

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

填充选择输入:jQuery-- Populate select from json

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多