Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 load data using ajax cannot select any option

I have the following code (javascript):

$('#cbxConnections').select2({
    minimumInputLength: 0,
    multiple: false,
    allowClear: true,
    placeholder:{
        text:"@Diccionario.Connections",
        id:" @Diccionario.Connections"
    },
    ajax:{
        url:'@Url.Action("GetActiveConnections","Admin")',
        dataType: 'json',
        type:'post',
        data:function(params){
            return {
                q: params.term
            };
        },
        processResults: function(data,page){
            return {
                results: data
            };
        }
    },
    escapeMarkup: function (markup) { 
        return markup; 
    },
    templateResult: function(response){
        return '<div>'+response.Name+'</div>';
    },
    templateSelection: function(response){
        return response.Id;
    },
    id: function(connection){
       console.log(connection);
    }
});

For the server side I am using ASP MVC 4. The select get data using ajax and render the options but this options are not selectable. Reading other posts, they describe using the id function, but this function appearently desappears on the version of select2 I'm using 2.4

I'm following the example of ajax on the documentation showing on github "Loading remote data"

like image 246
jcvegan Avatar asked Mar 13 '15 15:03

jcvegan


People also ask

Why select2 is not working?

select2 is not a function" jQuery error occurs for multiple reasons: Forgetting to include the select2 library. Loading the select2 library before the jQuery library. Loading the jQuery library twice.

How do I add options in select2?

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).trigger('change');

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.


1 Answers

If your ajax response doesn't have id and text attributes you should fix them client side

This is a requirement on version 4.0 (don't know why)

ajax: {

   processResults: function (data, params) {

                params.page = params.page || 1;

                // you should map the id and text attributes on version 4.0

                var select2Data = $.map(data.result.data, function (obj) {
                    obj.id = obj._id.$id;
                    obj.text = obj.name;

                    return obj;
                });

                return {
                    results: select2Data,
                    pagination: {
                        more: data.result.more
                    }
                };
            }

 }
like image 87
webmaster Avatar answered Sep 17 '22 00:09

webmaster