Hi,
I’m trying to create a dropdown list with checkboxes for a multiselect select2. I find some codes on the web, but all there not using ajax-url (data ou jquery) to search his data. In my case, the scripts work’s with some problem, his not unselecting…
Here the code I using:
jQuery(function($) {
$.fn.select2.amd.require([
'select2/selection/single',
'select2/selection/placeholder',
'select2/selection/allowClear',
'select2/dropdown',
'select2/dropdown/search',
'select2/dropdown/attachBody',
'select2/utils'
], function (SingleSelection, Placeholder, AllowClear, Dropdown, DropdownSearch, AttachBody, Utils) {
var SelectionAdapter = Utils.Decorate(
SingleSelection,
Placeholder
);
SelectionAdapter = Utils.Decorate(
SelectionAdapter,
AllowClear
);
var DropdownAdapter = Utils.Decorate(
Utils.Decorate(
Dropdown,
DropdownSearch
),
AttachBody
);
var base_element = $('.select2-multiple2')
$(base_element).select2({
placeholder: 'Selecione',
selectionAdapter: SelectionAdapter,
dropdownAdapter: DropdownAdapter,
allowClear: true,
templateResult: function (data) {
if (!data.id) { return data.text; }
var $res = $('<div></div>');
$res.text(data.text);
$res.addClass('wrap');
return $res;
},
templateSelection: function (data) {
if (!data.id) { return data.text; }
var selected = ($(base_element).val() || []).length;
var total = $('option', $(base_element)).length;
return selected + " de " + total;
}
})
});
});
(function(){
'use strict';
$.fn.select2.defaults.set('theme', 'bootstrap4')
$('[data-toggle="select"]').each(function() {
var element = $(this)
var options = {
dropdownParent: element.closest(".modal").length ? element.closest(".modal") : $(document.body),
minimumResultsForSearch: element.data("minimum-results-for-search"),
allowClear: true,
placeholder: "Pesquisar",
}
if (element.attr("multiple")){
element.addClass("select2-multiple2");
} else {
element.select2(options);
}
})
})()
And the aditional CSS:
.select2-results__option .wrap:before{
font-family: 'Font Awesome 5 Free';
color:#000;
content:"\f0c8";
width:25px;
height:25px;
padding-right: 10px;
}
.select2-results__option[aria-selected=true] .wrap:before{
content:"\f14a";
}
```
Thank's a lot!