我正在寻找一种在select2中创建子类别的方法,并在其中附加一个点击事件。现在我用这种方式格式化了我的结果:
问题是子类+ http链接只是我实现中select2中的一个元素。它并没有像我想要的那样运作。如何按照Select2功能(输出子类别而不是列表中的项目)的方式执行它?真的有可能吗?
这是我的代码:
function format2(item) {
return item.type;
}
function format1(item) {
var list = "<div>";
$.each(item.elements, function(index,value){
var url;
console.log(item.type);
switch(item.type) {
case 'Пост':
url = value.url;
break;
case 'Пользователь':
url = '/id' + value.ID;
break;
case 'Коллекция':
case 'Тэг':
case 'Категория':
url = '#';
break;
}
list += "<div>" + "<a href='" + url + "'>" + value.name + "</a>" + "</div>";
});
list += "</div>";
var markup = '<div>' + item.type + '<div>' + list + '</div>' + '</div>';
return markup;
}
$("input#s").select2({
multiple: true,
placeholder: "Search for a repository",
minimumInputLength: 3,
ajax: {
url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
dataType: 'json',
quietMillis: 250,
data: function (term, page) {
return {
action: 'roomble_load_term_posts',
search_param: term
};
},
results: function (data, page) {
var elements = [];
console.log(data);
$.each(data, function(index, value) {
if ((typeof value.posts != 'undefined') && value.posts.length > 0){
elements.push({elements: value.posts, type: "Пост"});
}
if ((typeof value.tags != 'undefined') && value.tags.length > 0){
elements.push({elements: value.tags, type: "Тэг"});
}
if ((typeof value.collections != 'undefined') && value.collections.length > 0){
elements.push({elements: value.collections, type: "Коллекция"});
}
if ((typeof value.categories != 'undefined') && value.categories.length > 0){
elements.push({elements: value.categories, type: "Категория"});
}
if ((typeof value.users != 'undefined') && value.users.length > 0){
elements.push({elements: value.users, type: "Пользователь"});
}
});
return {
results: elements
};
}
},
formatResult: format1,
formatSelection: format2,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
}).on("select2-selecting", function(e) {
console.log(e);
});