mor*_*ael 17 javascript ruby-on-rails jquery-select2
我正在使用Rails 4.2.6开发Ruby On Rails应用程序.我正在使用Turbolinks和jquery.turbolinks(抱歉,我不能发布链接到这些元素,因为我是网站上的新手).我的问题很简单,但我无法解决.这是:我有一个通过AJAX获取的表单
<div class="card-footer">
<a class="btn btn-sm btn-primary-outline" data-remote="true" href="/profiles/Mke5kA/positions/new"><i class="fa fa-plus"></i> Nouvelle expérience professionnelle</a>
<div id="new_position_form"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
表单包含通过AJAX获取数据的Select2元素
= simple_form_for [profile, position], remote: true, html: {id: 'positionForm', class: 'm-b-1'} do |f|
= f.input :company_id, as: :select, input_html: {:'data-behaviour' => 'company-select2', :'data-kind' => 'company'}
= f.input :title
= f.input :summary
- location = f.object.build_location
= f.simple_fields_for :location do |l|
= render 'locations/fields', l: l, city: position.city
= render "profiles/shared/date_fields", f: f, model: position
= f.input :skill_list, as: :select, input_html: {multiple: true, :data => {:behaviour => 'acts-as-taggable', :'taggable-context' => 'skills'}}
%button.btn.btn-primary{:type => "submit"}= icon('check-square-o', 'Enregistrer')
= link_to icon('remove', 'Annuler'), 'javascript:void(0)',
data: {:'lgnk-behaviour' => "remove-form", :'lgnk-target' => "#positionForm" }, class: 'btn btn-secondary'
Run Code Online (Sandbox Code Playgroud)
当我尝试输入使用AJAX获取数据的Select2时出现我的问题:所有select2都是重复的:
这是我如何初始化Select2:
var loc_tag = function() {
$('[data-behaviour="acts-as-taggable"]').not('.select2-hidden-accessible').each (function (index, element) {
if ($(element).data('value')) {
var options = $(element).data('value').split(', ');
$.each(options, function(key, tag){
$(element).append($('<option selected></option>').val(tag).text(tag));
});
}
$(element).select2({
ajax: {
url: "/tags?context="+$(element).data('taggable-context'),
dataType: 'json',
headers: {
"Accept": "application/json"
},
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 2,
tags: true,
language: "fr",
theme: "bootstrap",
width: "100%",
placeholder: 'Mots clés...'
});
});
};
$(document).on('page:load page:update', loc_tag);
Run Code Online (Sandbox Code Playgroud)
我希望Select2元素只被初始化一次(当提取表单时)而不是AJAX响应它们获取它们的数据.我已经尝试了jQuery.not(".select2-hiden-accessible")对未使用的元素Select2(select2-hidden-accessible是类Select2添加到初始化的Select2元素)但它不起作用.
非常感谢您的帮助!
Mat*_*att 31
使用Turbolinks 5和select2时,select2
对象不再连接(请参阅下面的测试)到<select>
使用后退按钮返回页面时.select2
返回后创建并附加一个新对象,但它无法使用.
杰克的回答对我不起作用,因为当select2
添加新对象时,<select>
仍有class='select2-hidden-accessible'
其他东西,其中包括width: 1px !important
.select2
创建新对象时,它基本上是不可见的.
对我来说,关键是在TL缓存页面之前销毁所有select2对象.这是适合我的解决方案:
$(document).on("turbolinks:before-cache", function() {
$('.select2-input').select2('destroy');
});
$(document).on('turbolinks:load', function() {
$('.select2-input').select2();
});
Run Code Online (Sandbox Code Playgroud)
我相信这是Turbolinks文档的正确方法(强调我的):
准备要缓存的页面
如果您需要在Turbolinks缓存之前准备文档,请收听turbolinks:before-cache事件.您可以使用此事件重置表单,折叠扩展的UI元素或拆除任何第三方窗口小部件,以便页面可以再次显示.
select2
实存要测试select2
对象是否附加到<select>
您,可以在控制台中执行以下操作:
('.select2-input').first().data('select2')
Run Code Online (Sandbox Code Playgroud)
2021 年更新:
使用turbolinks:before-cache
:
document.addEventListener('turbolinks:before-cache', function () {
// removing the select2 from all selects
$("select").select2('destroy');
});
Run Code Online (Sandbox Code Playgroud)
并在以下情况下加载 select2 turbolinks:load
:
document.addEventListener("turbolinks:load", function () {
// applying select2 to all selects
$("select").select2({
theme: 'bootstrap4' // if you want to pass some custom config
});
});
Run Code Online (Sandbox Code Playgroud)
当然,你可以创建一个自定义.select2-binder
类或者一个data-select2-binder
来选择哪个select会受到影响。
我遇到了同样的问题,我通过以下方式解决了:
// init.js (you can pass an container instead of using document
$( document ).on('turbolinks:load', function() {
$( document ).find('select').not('.select2-hidden-accessible').select2();
});
Run Code Online (Sandbox Code Playgroud)
现在我没有那些重复的选择:)
小智 5
我有同样的问题,我发现当你按下后退按钮时select
,select2
元素和元素都被渲染,但它们没有绑定在一起,所以当你用$('select).select2()
它重新初始化它时,它会select2
在它旁边创建另一个全新的元素。
所以这是我在初始化 select2 之前所做的:
如果select
不是 a select2
(ie $(el).data('select2') == undefined
) 但select2
它旁边已经有一个元素,则将其删除。
if ($(el).data('select2') == undefined && $(el).next().hasClass('select2-container')) {
$(el).next().remove();
}
$(el).select2();
Run Code Online (Sandbox Code Playgroud)