bil*_*oah 30 html jquery jquery-select2 jquery-select2-4
在这个从远程源加载的数据示例中,我可以看到图像和其他html元素呈现为选项.我想使用本地数组中的数据完成同样的事情.我已经尝试构建一个数组,如文档中所述并添加data选项,但html呈现为明文:
var data = [
{ id: 0, text: '<div style="color:green">enhancement</div>' },
{ id: 1, text: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>' }];
$("select").select2({
data: data
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select></select>Run Code Online (Sandbox Code Playgroud)
如何将html内容添加到select2选项?
bil*_*oah 30
好的,玩了一段时间,找到了一个有效的解决方案,所以我会在这里回答我自己的问题.
这里对我来说,关键是要建立与两个内容的数据阵列templateSelection和templateResult.后者在下拉列表中呈现查找但是任何多行内容都不会包含在select2元素中,因此需要内联显示(或至少在一行上).定义escapeMarkup为一个选项允许覆盖通常会删除html内容的核心功能.
定义title属性也很重要,否则你最终会在工具提示中找到html标签.
var data = [{
id: 0,
text: '<div style="color:green">enhancement</div>',
html: '<div style="color:green">enhancement</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>',
title: 'enchancement'
}, {
id: 1,
text: '<div style="color:red">bug</div>',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>',
title: 'bug'
}];
$("select").select2({
data: data,
escapeMarkup: function(markup) {
return markup;
},
templateResult: function(data) {
return data.html;
},
templateSelection: function(data) {
return data.text;
}
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select></select>Run Code Online (Sandbox Code Playgroud)
或者,通过几个小的CSS调整,您可以允许完整的html选项内容显示在select容器内,而无需模板回调:
var data = [{
id: 0,
text: '<div style="font-size: 1.2em; color:green">enhancement</div><div><b>Select2</b> supports custom themes using the theme option so you can style Select2 to match the rest of your application.</div>',
title: 'enchancement'
}, {
id: 1,
text: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>',
title: 'bug'
}];
$("select").select2({
data: data,
escapeMarkup: function(markup) {
return markup;
}
})Run Code Online (Sandbox Code Playgroud)
.select2-container .select2-selection--single {
height: auto!important;
padding: 5px 0;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
line-height: normal!important;
}
.select2-container .select2-selection--single .select2-selection__rendered {
white-space: normal!important;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select style="width: 100%"></select>Run Code Online (Sandbox Code Playgroud)
apo*_*fos 28
如果我没有弄错,只有设置了templateResult和templateSelection选项并让它们返回一个jQuery对象,才能呈现HTML.
var data = [
{ id: 0, text: '<div style="color:green">enhancement</div>' },
{ id: 1, text: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>' }];
$("select").select2({
data: data,
templateResult: function (d) { return $(d.text); },
templateSelection: function (d) { return $(d.text); },
})Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script>
<select></select>
Run Code Online (Sandbox Code Playgroud)
只需将html的另一个字段添加到数据数组中,然后使用templateResult类似的选项 创建自己的模板
JSFiddle Demovar data = [{
id: 0,
text: 'enhancement',
html: '<div style="color:green">enhancement</div>'
}, {
id: 1,
text: 'bug',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}];
function template(data) {
return data.html;
}
$("select").select2({
data: data,
templateResult: template,
escapeMarkup: function(m) {
return m;
}
});
Run Code Online (Sandbox Code Playgroud)
另一个例子是这样定义的
function template(data) {
if ($(data.html).length === 0) {
return data.text;
}
return $(data.html);
}
$("select").select2({
ajax: {
url: 'routeurl',
dataType: 'json',
type: 'POST',
processResults: function(data) {
return {
results: data
};
},
cache: true
},
allowClear: true,
placeholder: 'Select at least one element',
templateResult: template,
templateSelection: template
});
Run Code Online (Sandbox Code Playgroud)
格式为 json 的端点结果如此
[{
id: 0,
text: 'enhancement',
html: '<div style="color:green">enhancement</div>'
}, {
id: 1,
text: 'bug',
html: '<div style="color:red">bug</div><div><small>This is some small text on a new line</small></div>'
}, {
id: 2,
text: 'success',
html: 'Success'
}]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42942 次 |
| 最近记录: |