以下是视频相关的 select2 ajax 获取后台数据的 Js 代码:
$(".js-example-basic-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: formateTopic,
templateSelection: formatTopicSelection,
escapeMarkup: function (markup) {
return markup;
}
});