【问题标题】:Activeadmin form select dropdown updateActiveadmin 表单选择下拉更新
【发布时间】:2014-10-05 16:37:56
【问题描述】:

我有一个类别模型和一个子类别模型,并且我有一个子类别模型和 ID,如要根据与我选择的特定类别关联的子类别进行刷新的子类别选择输入。

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 activeadmin


    【解决方案1】:

    您可以为此目的使用依赖选择。 示例给出here

    活跃的管理员

    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
    

    目录控制器

    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
    

    【讨论】:

    • 如果你很懒,只想制作字符串,试试这个:f.input :status, as: :select, collection: (["new", "checked_in", "called","complete"])
    【解决方案2】:
    1. 您必须在ActiveAdmin Category 模型中创建一个成员操作(方法:GET,参数:所选Category 的id),该模型返回SubCategories(例如json)属于所选Category:

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

    2. 您必须使用 jQuery(例如)和 ajax,当类别选择输入已更改时填充子类别选择输入:

      Populate Select box options on click with Javascript/Jquery with Json data https://forum.jquery.com/topic/best-practices-for-populating-selects-with-ajax

    【讨论】:

      【解决方案3】:

      你可以这样做来添加特定的选择

      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
      

      【讨论】:

        猜你喜欢
        • 2017-11-13
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 2019-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多