I’m new to select2. Moving over from jquery autocomplete.
So I have everything working perfect. The search works, displays nicely with the image thumbnail…except nothing happens when I click the item I want.
I’m trying to return the name of the category but it’s not working. I could really use someone to point me in the right direction. Thanks
$(".js-data-example-ajax").select2({
ajax: {
url: 'index.php?route=catalog/category/autocomplete_v2&user_token={{ user_token }}',
type: 'post',
dataType: 'json',
delay: 250,
data: function(params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function(results, params)
{
console.log('processResults results', results);
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: results.data,
//pagination:
//{
// more: (params.page * 30) < results.recordsTotal
//}
};
},
cache: false
},
escapeMarkup: function(markup)
{
return markup;
},
minimumInputLength: 1,
templateResult: formatCategory,
templateSelection: function (category) {
return category.name;
}
});
function formatCategory(category) {
if (category.loading)
{
return category.text;
}
console.log('formatRepo category', category);
var markup = "<div class='select2-result-categoriessitory clearfix d-flex'>" +
"<div class='select2-result-categoriessitory__avatar mr-2'><img src='" + category.image + "' class='width-2 height-2 mt-1 rounded' /></div>" +
"<div class='select2-result-categoriessitory__meta'>" +
"<div class='select2-result-categoriessitory__title fs-lg fw-500'>" + category.name + "</div>";
if (category.description_txt)
{
markup += "<div class='select2-result-categoriessitory__description fs-xs opacity-80 mb-1'>" + category.description_txt + "</div>";
}
markup += "</div></div>";
return markup;
}