I\'m having trouble using the Select2 with various groups, only the latter appears.
I tried adding this as a comment to Saravanan's post above, but the length of the comment was too long, so consider this as an expansion to his post, and credit goes to him for giving me the idea.
This is a bit of a necro post from me, but just wanted to expand on how I implemented the above solution with the newer jquery format of $document.ready instead of $document.on. Slightly modified as well, since I have mine nested in a pageLoad, and thus the function is outside the pageLoad, and I used an attribute rather than a class. Important part though, is that I had to put both templateResult and templateSelection for it to work, as without the latter, nothing happened:
function pageLoad() {
$(document).ready(function () {
$(".multiple-group").select2({
allowClear: true,
closeOnSelect: false,
templateResult: formatResult,
templateSelection: formatResult
});
});
}
function formatResult(node) {
var level = 0;
if (node.element !== undefined) {
level = node.element.getAttribute("hierarchy-level");
if (level.trim() !== '') {
level = parseInt(level) - 1;
}
}
var $result = $('' + node.text + '');
return $result;
}