【发布时间】:2020-09-16 10:46:38
【问题描述】:
我有这个带有 Select2 的 Jexel 代码,我从数组中加载每个 jexcel 行的默认值,并使用 select2 作为下拉菜单: https://jsfiddle.net/ktumw528/
但是我想知道如何使用 var 数据中的值填充 select2 中的选项? 另外,如果键入时未找到,如何将新国家/地区添加到 select2 下拉列表中。
var data = [
['Spain'],
['France'],
['Germany'],
];
var customDropDown1 = {
// Methods
closeEditor : function(cell, save) {
// Get value
var txt = $(cell).find('.editor').val();
// Set visual value
$(cell).html(txt);
// Close edition
$(cell).removeClass('edition');
// Save history
return txt;
},
openEditor : function(cell) {
// Get current content
var currentValue = $(cell).text();
// Create the editor
var editor = document.createElement('select');
$(cell).html(editor);
$(editor).prop('class', 'editor');
$(editor).html('<option>Brazil</option><option>Canada</option>')
$(editor).val(currentValue);
// Create the instance of the plugin
$(editor).select2().on('select2-blur', function() {
$('#' + $.fn.jexcel.current).jexcel('closeEditor', $(cell), true);
});
},
getValue : function(cell) {
return $(cell).text();
},
setValue : function(cell, value) {
$(cell).html(value);
return true;
}
}
$('#my').jexcel({
data:data,
columns: [ { editor:customDropDown1 } ],
colHeaders: ['Country'],
colWidths: [ 300 ]
});
欢迎任何提示:) 非常感谢!
【问题讨论】:
标签: javascript jquery jquery-select2 jquery-select2-4 jexceljs