Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 undefined issue

The following function gets json data from URL and populates a select element with jQuery. I am than using Select2 to transform that drop down in a field with autocomplete function.

Everything works fine apart from the writing 'undefined' that I get as soon as the select elements get displayed. The autocomplete and drop down work perfectly. I have tried to use the data placeholder even by adding an empty 'option' element but no success.

function CitiesList(callback){
 $.getJSON(document.URL+'getCities/sdfsfs', function(data){
    var html = '';
    var len = data.length;
    var option = '<option></option>';
    for (var i = 0; i< len; i++) {
        html += '<option value="' + data[i] + '">' + data[i] + '</option>';
    }
    $('.select_cities select').append(option);
    $('.select_cities select').append(html);
    if(callback && typeof callback == 'function'){
        callback.call(null);
    }

});
}

            <select data-placeholder="Select a city" name="cities" id="cities">
            </select>

'select_cities' is the div wrapper around the select element.

like image 633
Aessandro Avatar asked Jul 24 '13 10:07

Aessandro


People also ask

How do I update Select2 options?

How do I update Select2 options? New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).

What does Select2 () do?

Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

How do I contact Select2?

Select2 will register itself as a jQuery function if you use any of the distribution builds, so you can call .select2() on any jQuery selector where you would like to initialize Select2.

How does Select2 search work?

When users filter down the results by entering search terms into the search box, Select2 uses an internal "matcher" to match search terms to results. You may customize the way that Select2 matches search terms by specifying a callback for the matcher configuration option.


3 Answers

This is working now in 4.0. The message sent to the results formatter can be configured either in the object (see their GitHub example repo.text) or hard coded within the function that is called from the templateResult property. e.g.:

templateResult: formatResult,

//...

function formatResult (result) {

if (result.loading) return "Searching...";

Configuration wasn't the easiest thing to find since looking for the string "Searching..." within their their GitHub example had zero hits, but in retrospect is quite intuitive.

like image 145
Julie McMurry Avatar answered Oct 07 '22 23:10

Julie McMurry


I ran into this recently. It looks like newer versions of Select2 have trouble when a default option is set without a corresponding value or when value is an empty string, for example:

<option value="" selected="selected">Select something</option>

Either explicitly define a non empty value such as value="0" or set placeholderOption: 'first' to force Select2 to use the first option as a default:

$('#select2-field').select2({
    placeholderOption: 'first'
});
like image 33
nurikabe Avatar answered Oct 07 '22 21:10

nurikabe


This issue has been addressed in the latest (3.4.2) version of select2. Upgrade and you should be good to go!

like image 23
James Lai Avatar answered Oct 07 '22 23:10

James Lai