简体   繁体   English

Activeadmin 表单 select 下拉更新

[英]Activeadmin form select dropdown update

I have a Category Model and a SubCategory Model and id like the SubCategory select input to refresh depending on the SubCategories that are associated to the specific Category I have selected.我有一个类别 Model 和一个子类别 Model 和 id 像子类别 select 输入以刷新取决于我选择的特定类别的子类别。

form do |f|
    f.inputs do
        f.input :title
        f.input :category, as: :select, collection: Category.all, :input_html => { :class => 'chzn-select', :width => 'auto', "data-placeholder" => 'Click' }
        f.input :sub_category, as: :select, collection: SubCategory.all, :input_html => { :class => 'chzn-select', :width => 'auto', "data-placeholder" => 'Click' }
    end
    f.actions
end

You can use dependent select for this purpose.为此,您可以使用相关选择。 Example is given here示例在此处给出

Active Admin活跃管理员

ActiveAdmin.register CatalogsProduct do
  form do |f|
    f.inputs "Details" do
      f.input :product, :as => :select, :collection => Product.all.collect {|product| [product.name, product.id] }
      f.input :catalog, :as => :select, :input_html => {'data-option-dependent' => true, 'data-option-url' => '/products/:catalogs_product_product_id/catalogs', 'data-option-observed' => 'catalogs_product_product_id'}, :collection => (resource.product ? resource.product.category.catalogs.collect {|catalog| [catalog.attr_name, catalog.id]} : []) 
    end
    f.actions
  end
end

Catalog controller目录控制器

class CatalogsController < ApplicationController
  respond_to :json

  def index
    if params[:product_id]
      product = Product.find_by_id(params[:product_id])
      @catalogs = product.category.catalogs
    else
      @catalogs = Catalog.all
    end
    render :json => @catalogs.collect {|catalog| {:id => catalog.id, :name => catalog.attr_name} }
  end
end
  1. You have to create a member action (method: GET, params: the id of the selected Category) in the ActiveAdmin Category model that returns the SubCategories (in json, for example) belongs to the selected Category:您必须在 ActiveAdmin 类别模型中创建一个成员操作(方法:GET,参数:所选类别的 id),返回属于所选类别的子类别(例如,在 json 中):

    https://github.com/activeadmin/activeadmin/blob/master/docs/8-custom-actions.md#member-actions https://github.com/activeadmin/activeadmin/blob/master/docs/8-custom-actions.md#member-actions

  2. You have to use jQuery (for exmaple) with ajax, that populate the SubCategory select input when the Category select input has been changed:您必须使用带有 ajax 的 jQuery(例如),当 Category 选择输入已更改时,它会填充 SubCategory 选择输入:

    Populate Select box options on click with Javascript/Jquery with Json data https://forum.jquery.com/topic/best-practices-for-populating-selects-with-ajax 使用带有 Json 数据的 Javascript/Jquery 填充选择框选项https://forum.jquery.com/topic/best-practices-for-populating-selects-with-ajax

you can do something like this to add specific selection你可以做这样的事情来添加特定的选择

form do |f|
            f.inputs do
            f.input :category , :as => :select, collection: (['conversion','double-bar', 'fixed-hybrid','removal-bar-over-denture', 'surgical-guides'])
  
            end
            actions 
          end

You can apply this, I did it by relating games to categories and subcategories.你可以应用这一点,我通过将游戏与类别和子类别相关联来做到这一点。 Working on Ruby on Rails 6.0.4 and Active Admin 2.9.0.在 Rails 6.0.4 和 Active Admin 2.9.0 上使用 Ruby。

Models楷模

class Game < ApplicationRecord 
  belongs_to :category
  belongs_to :sub_category
end

class Category < ApplicationRecord 
  has_many :sub_categories
end

class SubCategory < ApplicationRecord 
  belongs_to :category
end

Active admin form活动管理表格

ActiveAdmin.register Game do
  form do |f|
    f.inputs "Details" do
      f.input :category_id, as: :select, collection: Category.all
      f.input :sub_category_id, as: :select, collection: ([]) # The input is initialized without values
    end
    f.actions
  end
end

You must do the following:您必须执行以下操作:

  1. Create a controller, that returns the subcategories.创建一个返回子类别的 controller。

     class SubCategoriesController < ApplicationController def sub_category_filter if params[:category_id] category = Category.find(params[:category_id]) @sub_categories = category.sub_categories else @sub_categories = [] end render:json => @sub_categories.collect {|sub_category| {:id => sub_category.id, :name => sub_category.name} } end end
  2. Add the route in the routes.rb file.routes.rb文件中添加路由。

     get 'sub_category_filter/' => 'sub_category#sub_category_filter'
  3. Inspect with your web browser console your selects, and use a CSS selector to create a jQuery object for the category select, something like: Inspect with your web browser console your selects, and use a CSS selector to create a jQuery object for the category select, something like:

     $('#game_category_id')
  4. Add this block of code in the file /app/assets/javascripts/active_admin.js , which is responsible for making the call to the generated controller, and adding the options that it returns.在文件/app/assets/javascripts/active_admin.js中添加这段代码,该文件负责调用生成的 controller,并添加它返回的选项。

     //= require active_admin/base $(function () { $('#game_category_id').on('change', function () { $('#game_sub_category_id option').remove(); // Remove all <option> child tags. $.getJSON(`/sub_category_filter`, {category_id: $(this).val()}, function(result){ // Documentation on getJSON: http://api.jquery.com/jQuery.getJSON/ $.each(result, function (i, item) { // Iterates through a collection $('#game_sub_category_id').append($('<option>', { // Append an object to the inside of the select box value: item.id, text: item.name })); }); }); }) })

References:参考:

Can't find the error in my dependent select drop down on Active Admin( Rails 3.2, Active Admin 1.0) 在我的依赖 select 在 Active Admin 下拉菜单中找不到错误(Rails 3.2,Active Admin 1.0)

Populate Select box options on click with Javascript/Jquery with Json data 使用带有 Json 数据的 Javascript/Jquery 填充 Select 框选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM