下面的一些代码可以直接使用,出现不能正常运行的情况的话仔细检查 css class
,本代码适用于 https://www.laravist.com/series/build-a-zhihu-website-with-laravel/episodes/11
function formatTopic (topic) {
return "<div class='select2-result-repository clearfix'>" +
"<div class='select2-result-repository__meta'>" +
"<div class='select2-result-repository__title'>" +
topic.name ? topic.name : "Laravel" +
"</div></div></div>";
}
function formatTopicSelection (topic) {
return topic.name || topic.text;
}
$(".js-example-placeholder-multiple").select2({
tags: true,
placeholder: '选择相关话题',
minimumInputLength: 2,
ajax: {
url: '/api/topics',
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term
};
},
processResults: function (data, params) {
return {
results: data
};
},
cache: true
},
templateResult: formatTopic,
templateSelection: formatTopicSelection,
escapeMarkup: function (markup) { return markup; }
});