gpt4 book ai didi

javascript - AMD/select2 行为差异?

转载 作者:行者123 更新时间:2023-12-02 23:09:37 25 4
gpt4 key购买 nike

我正在尝试修改此处的 select2/AMD 示例:https://stackoverflow.com/a/31600521/567736我不确定我遇到的问题是否与 select2 有关,或者我是否误解了 AMD。我的代码:

$.fn.select2.amd.define("InlineSearchSelectionCustomAdapter", [
"select2/utils",
"select2/selection/multiple",
"select2/dropdown/search",
],
function (Utils, MultipleSelection, Search) {
let adapter = Utils.Decorate(
MultipleSelection,
Search
);

return adapter;
}
);

$.fn.select2.amd.require([
'select2/selection/multiple',
'select2/selection/search',
'select2/utils',
'InlineSearchSelectionCustomAdapter'
], function (MultipleSelection, Search, Utils, InlineSearchSelectionCustomAdapter) {

let SelectionAdapter = Utils.Decorate(
MultipleSelection,
Search
);

$('.inline-search-original').select2({
selectionAdapter: SelectionAdapter
});

$('.inline-search-amd').select2({
selectionAdapter: InlineSearchSelectionCustomAdapter
});
});
.select2-selection__choice__remove {
display: none !important;
}

.select2-container--focus .select2-autocomplete .select2-selection__choice {
display: none;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet" />

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.js"></script>

<p>With an inline search box, original</p>
<select style="width: 200px" class="inline-search-original" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>

<p>With an inline search box, using amd</p>
<select style="width: 200px" class="inline-search-amd" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>

如你所见,我正在尝试重构SelectionAdapter进入一个单独的模块InlineSearchSelectionCustomAdapter 。但是,我的新 <select> 的外观和行为与原始 stackoverflow 帖子不同。

为什么行为不同以及如何纠正错误?

最佳答案

导入 select2/selection/search 而不是 select2/dropdown/search

$.fn.select2.amd.define("InlineSearchSelectionCustomAdapter", [
"select2/utils",
"select2/selection/multiple",
"select2/selection/search",
],
function (Utils, MultipleSelection, Search) {
let adapter = Utils.Decorate(
MultipleSelection,
Search
);

return adapter;
}
);

$.fn.select2.amd.require([
'select2/selection/multiple',
'select2/selection/search',
'select2/utils',
'InlineSearchSelectionCustomAdapter'
], function (MultipleSelection, Search, Utils, InlineSearchSelectionCustomAdapter) {

let SelectionAdapter = Utils.Decorate(
MultipleSelection,
Search
);

$('.inline-search-original').select2({
selectionAdapter: SelectionAdapter
});

$('.inline-search-amd').select2({
selectionAdapter: InlineSearchSelectionCustomAdapter
});
});
.select2-selection__choice__remove {
display: none !important;
}

.select2-container--focus .select2-autocomplete .select2-selection__choice {
display: none;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet" />

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.js"></script>

<p>With an inline search box, original</p>
<select style="width: 200px" class="inline-search-original" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>

<p>With an inline search box, using amd</p>
<select style="width: 200px" class="inline-search-amd" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>

关于javascript - AMD/select2 行为差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com