Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 - initselection error

I am getting the following error when using select2 plugin on initSelection.

Error: Uncaught Error: No select2/compat/initSelection is the error I am getting.

Hope somebody helps me in sorting out the inistselection issue.

JS:

  $(".doctor_id_pat").select2({
    placeholder: "Search Users",
    minimumInputLength: 2,
    ajax: {
        url: "/listallergiesajax",
        dataType: 'json',
        data: function(term) {
            return {
                q: term
            };
        },
        results: function(data, page) {
            return {

                results: $.map(data, function(item) {

                    return {
                        text: item.text,
                        id: item.id
                    }
                })

            };
        },
    },
    initSelection: function(element, callback) {

        $.ajax("/listallergiesajax", {
            dataType: "json"
        }).done(function(data) {

            callback(data.results[0]);

        });
    }
});
like image 383
mark winkle Avatar asked Apr 28 '15 12:04

mark winkle


People also ask

What is initSelection in Select2?

In the past, Select2 required an option called initSelection that was defined whenever a custom data source was being used, allowing for the initial selection for the component to be determined. This has been replaced by the current method on the data adapter.

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.

Is Select2 a plugin?

Select2 is a powerful JQuery plugin that is designed to add more power to the traditional <select> box that is displayed on the browser. It supports searching, remote data-sets via AJAX, mult-selects, pagination, grouping, custom rendering and so much more.


1 Answers

Select2 v3.5.2 uses the initSelection method. Select2 v4 has a different approach.

For information on how to upgrade (initSelection and other stuff) see https://select2.org/upgrading/migrating-from-35#removed-the-requirement-of-initselection.

like image 168
joost Avatar answered Oct 08 '22 08:10

joost