【发布时间】:2022-02-17 21:55:27
【问题描述】:
我正在使用jQuery UI 工具提示和Select2。我的代码如下。
$(".volunteer").on("click", function (event) {
function templateSelection(data_format) {
if (!data_format.id) { return data_format.text; }
var output = $('<span class="tooltip" title="' + data_format.title + '">' + data_format.text + '</span>');
return output;
};
function resultfucntion(state) {
if (!state.id) { return state.text;}
var $state = $('<span class="tooltip" title="' + state.title + '">' + state.text + '('+ state.text1 +')</span>');
return $state;
};
var orga_id = $('#orga').val();
var cellEle = $(this);
// Populate select element
cellEle.html(`<select multiple="multiple"></select>`);
// Initialise select2
let selectEle = cellEle.children("select").select2({
ajax: {
url: "/wp-admin/admin-ajax.php",
data: function (params) {
return {
action: 'get_data',
search: params.term,
orga_id: orga_id,
};
},
processResults: function (data) {
var options = [];
if (data) {
$.each(data, function (index, text) {
var user_data = '<table> \
<tr> \
<td>Organisation</td> \
<td>'+text[2][1]+'</td> \
</tr> \
<tr> \
<td>Age</td> \
<td>'+ text[2][0]+'</td> \
</tr> \
</table>';
options.push({ id: index, text: text[0], text1: text[1], title: user_data });
});
}
return {
results: options,
more: false
};
},
},
templateSelection: templateSelection,
templateResult: resultfucntion,
});
});
selectEle.on("select2:opening", function (e) {
$(document).on("mouseenter", ".select2-results__option", function () {
$(this).tooltip({
position: {
my: "center bottom-20",
at: "center top",
using: function (position, feedback) {
$(this).css(position);
var txt = $(this).text();
$(this).html(txt);
$("<div>")
.addClass("arrow")
.addClass(feedback.vertical)
.addClass(feedback.horizontal)
.appendTo(this);
},
},
});
});
});
selectEle.on("select2:closing", function (e) {
$('.select2-results__option').tooltip('close'); // This code is not working.
$('.select2-results__option .tooltip').tooltip('close'); // This code is not working.
});
selectEle.on("select2:select", function () {
$(".select2-results__option").tooltip("close"); // This code is not working.
$(".select2-results__option .tooltip").tooltip("close"); // This code is not working.
});
选择值后工具提示未关闭。
我在控制台中收到以下错误消息。
$(".volunteer").on("click", function (event) {
function templateSelection(data_format) {
if (!data_format.id) { return data_format.text; }
var output = $('<span class="tooltip" title="' + data_format.title + '">' + data_format.text + '</span>');
return output;
};
function resultfucntion(state) {
if (!state.id) { return state.text;}
var $state = $('<span class="tooltip" title="' + state.title + '">' + state.text + '('+ state.text1 +')</span>');
return $state;
};
var orga_id = $('#orga').val();
var cellEle = $(this);
// Populate select element
cellEle.html(`<select multiple="multiple"></select>`);
// Initialise select2
let selectEle = cellEle.children("select").select2({
ajax: {
url: "/wp-admin/admin-ajax.php",
data: function (params) {
return {
action: 'get_data',
search: params.term,
orga_id: orga_id,
};
},
processResults: function (data) {
var options = [];
if (data) {
$.each(data, function (index, text) {
var user_data = '<table> \
<tr> \
<td>Organisation</td> \
<td>'+text[2][1]+'</td> \
</tr> \
<tr> \
<td>Age</td> \
<td>'+ text[2][0]+'</td> \
</tr> \
</table>';
options.push({ id: index, text: text[0], text1: text[1], title: user_data });
});
}
return {
results: options,
more: false
};
},
},
templateSelection: templateSelection,
templateResult: resultfucntion,
});
});
selectEle.on("select2:opening", function (e) {
$(document).on("mouseenter", ".select2-results__option", function () {
$(this).tooltip({
position: {
my: "center bottom-20",
at: "center top",
using: function (position, feedback) {
$(this).css(position);
var txt = $(this).text();
$(this).html(txt);
$("<div>")
.addClass("arrow")
.addClass(feedback.vertical)
.addClass(feedback.horizontal)
.appendTo(this);
},
},
});
});
});
selectEle.on("select2:closing", function (e) {
$('.select2-results__option').tooltip('close'); // This code is not working.
$('.select2-results__option .tooltip').tooltip('close'); // This code is not working.
});
selectEle.on("select2:select", function () {
$(".select2-results__option").tooltip("close"); // This code is not working.
$(".select2-results__option .tooltip").tooltip("close"); // This code is not working.
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<table style="text-align:left;min-width:800px;margin-top: 0 !important;">
<tbody><tr>
<th style="min-width:100px">Task <a href="/team/?task=&team=turramurra-team">+</a></th>
<th style="min-width:150px">Time</th>
<th style="min-width:180px">volunteers</th>
<th style="width:50%">Comment</th></tr><tr>
<td><a href="/team/?task=test-1&team=turramurra-team" title="Edit this task">test 1</a></td>
<td>09:00 - 12:00</td>
<td class="volunteer" id="1485"></td>
<td><p>hello</p>
</td>
</tr><tr>
<td><a href="/team/?task=test2&team=turramurra-team" title="Edit this task">test2</a></td>
<td>09:00 - 12:00</td>
<td class="volunteer" id="1486"></td>
<td><p>test</p>
</td>
</tr></tbody></table>
问题的三种状态。
第一个状态,当我点击 td 并显示 select2 下拉菜单时。
第二个状态,当我将鼠标悬停在 select2 下拉列表的值上并显示工具提示时。
第三 状态,当我单击 select2 的值并选择了该值但工具提示仍在显示时。我需要 close、remove 或 destroy 工具提示。我也可以在控制台中看到一条错误消息。
我正在尝试复制我的问题here。但是我对 jsfiddle 不是那么专家,这就是为什么我不能正确地做到这一点。
【问题讨论】:
-
请使用code snippet 提供minimal-reproducible-example。因此,人们可以运行您的代码并帮助轻松调试。
-
看看我提供的链接,有一些指南可以教你制作。
-
您能否创建一个 工作 sn-p(或修复现有的)来重现该问题?
-
您可以在此处查看 jsfiddle 如何处理 fetch:jsfiddle.net/moshfeu/3hrupdn1/22 和 docs。
-
我明白,但为了调查,很容易获得一个真正的演示并玩它并尝试一下,这对我来说是最有效的。
标签: javascript jquery jquery-select2 jquery-ui-tooltip