ruby-on-rails Select2未在下拉菜单中显示搜索选项?

y0u0uwnf  于 2023-05-08  发布在  Ruby
关注(0)|答案(2)|浏览(139)

我已经将select 2与我的活动管理员集成。但它工作不正常。它只显示下拉菜单,没有搜索选项。
我已经按照以下链接:(Select2-rails is not working with ActiveAdmin
使用的Ruby版本:ruby-2.6.3 [ x86_64 ] Rails版本:Rails 5.0.7.2
这是我的app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require_tree .
//= require select2
//= require select2_locale_pt-BR
$( "#dropdown" ).select2({
language: "zh-TW"
});

我的app/assets/stylesheets/application.css

*= require bootstrap
 *= require_tree .
 *= require_self
 *= require select2
 *= require select2-bootstrap

我在这里使用了select 2,

customer_location.input :area, as: :select2, collection: Region.all.map {|u| [u.area.to_s, u.id]}, :input_html => { :class => 'select2-input', :width => 'auto', "data-placeholder" => 'Select Area' }
mxg2im7a

mxg2im7a1#

我用的是activeadmin-select2gem,对我来说很好用。
activeadminjquery-railsselect2-rails添加到Gemfile中:

gem 'activeadmin'
gem 'jquery-rails'
gem 'select2-rails'

然后将activeadmin-select2添加到Gemfile中:
gem 'activeadmin-select2', github: 'mfairburn/activeadmin-select2'
使用以下命令将activeadmin-select 2调用添加到active_admin样式表和javascript:
@import "active_admin/select2/base";#add this line to your app/assets/stylesheets/active_admin.scss
#= require active_admin/select2#add this line to your app/assets/javascripts/active_admin.js

用法:
过滤器

标准:选择过滤器将自动转换为Select 2过滤器。如果你想要一个多选组合框,那么用途:

ActiveAdmin.register Products do
      filter :fruits, as: :select2_multiple, collection: [:apples, :bananas, :oranges]
   end

选择列表

要使用Select 2样式列表,只需将:select更改为:select 2或:select2_multiple

ActiveAdmin.register Products do
     form do |f|
       f.input :fruit, as: :select2
     end
     form do |f|
       f.inputs "Product" do
         f.has_many :fruits, allow_destroy: true, new_record: "Add Fruit" do |e|
           e.input :fruit, as: :select2_multiple
         end
       end
     end
   end
7eumitmz

7eumitmz2#

对我来说,我解决了这个问题,通过应用这个gem与select2 gem在gem文件中的搜索选项出现在下拉列表中的活动管理

gem 'activeadmin-searchable_select'

在活动管理中添加

f.input :address_id, label: "Client Address" , as: :searchable_select, :collection => Address.order(name: :desc).to_a(&:name).collect {|address| [address.name, address.id] }

相关问题