Can't add placeholder to select2 4.0.3 with AJAX for data loading.

1,792 views
Skip to first unread message

na...@bigdrop.io

unread,
Feb 7, 2017, 3:47:01 PM2/7/17
to select2, Ashish Kohli
Hey!

I am using select2 4.0.3 in the following manner:

<div class="form-group" >
<select class="form-control course" id="id_populate_course_categories" name="course">
<option value="-1" selected="selected">Select an option</option>
</select>
</div>


And my js is as follows:

$("#id_populate_course_categories").select2({
placeholder: {
id: "-1",
placeholder: "Select an option"
},
allowClear: true,
ajax: {
url: "populate/course/categories/",
dataType: 'json',
delay: 0,
data: function (params) {
return {
q: params.term, // search term
page: $('input[name=level]:checked').val(),
};
},
processResults: function (data, params) {
// 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: data,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) {
return markup;
},
"language": {
"noResults": function(){
return "No Courses Found. Try Another Query? ";
}
},
minimumInputLength: 3,
templateResult: formatCourseCategory,
templateSelection: formatCourseCategorySelection,
}).on("change", function (e) {
var option_chosen = $("#select2-id_populate_course_categories-container").text();
$("input[name='course_type']").val(option_chosen);
console.log($(this).text());
// $('select[name=q_exm]').empty().append('<option value="0">Your Qualifying Exam?</option>');
// $('input[name="q_exm_score"]').hide();
// populate_qualifying_exam(course_option_chosen = option_chosen)
});


I have been unable to make the placeholder show and have tried every possible option. I am adding the placeholder exactly as demonstrated in the 4.0.3 release notes, and yet unable to make it work.

na...@bigdrop.io

unread,
Feb 7, 2017, 4:01:46 PM2/7/17
to select2, ashishk...@gmail.com, na...@bigdrop.io

Hey! I found a solution, and am adding it just in case some else faces this issue.

Basically, the SO answer here: http://stackoverflow.com/questions/30302329/select2-4-0-placeholder-undefined
helped me, looking at this jsfiddle: https://jsfiddle.net/xqhp0z0x/1/

In my code, templateSelection pointed to function formatCourseCategorySelection, which was this:


function formatCourseCategorySelection(repo) {
return repo.name
}

changing this to

function formatCourseCategorySelection(repo) {
return repo.full_name || repo.text;
}

worked.

I believe this is only applicable for select2 4.0.0 and onwards. Also, version 4 does not need an empty option in the select element, as opposed to previous releases. ( they couldn't have possible made this any more difficult to learn )

Phill Jones

unread,
Oct 21, 2025, 11:50:44 AM (9 days ago) Oct 21
to select2
Joann Fabrics products include a wide range of yarns in every weight, from super fine to super bulky, perfect for all types of projects. Popular brands such as Red Heart, Caron, Big Twist, and Lion Brand are available in many colors and textures. With high-quality materials and endless options, Joann Fabrics products inspire creativity for every crafter.
Reply all
Reply to author
Forward
0 new messages