Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 TypeError: data.results is undefined

Am trying to use Select2 to load remote data using ajax / json but i keep getting an error as:

TypeError: data.results is undefined

My code is :

$('#tags').select2({
                ajax: {
                    url: 'http://localhost:8090/getallusers',
                    dataType: 'json',
                    quietMillis: 100,
                    data: function (term) {
                        return {
                            term: term
                        };
                    },
                    results: function (data) {
                        return data;
                        }

                    }

            });

I really Don’t Understand the Problem !

like image 589
test Avatar asked Nov 29 '13 11:11

test


1 Answers

Select2 needs the results as a collection of objects with id: and text: attributes.

Like:

[{ 'id': 1, 'text': 'Demo' }, { 'id': 2, 'text': 'Demo 2'}]

Try, to reformat you response like:

$('#tags').select2({
    ajax: {
        url: 'http://localhost:8090/getallusers',
        dataType: 'json',
        quietMillis: 100,
        data: function (term) {
            return {
                term: term
            };
        },
        results: function (data) {
            var myResults = [];
            $.each(data, function (index, item) {
                myResults.push({
                    'id': item.id,
                    'text': item.first_name + " " + item.last_name
                });
            });
            return {
                results: myResults
            };
        }
    }
});
like image 123
Irvin Dominin Avatar answered Oct 26 '22 19:10

Irvin Dominin