yash1996
yash1996

Reputation: 161

Results not displaying using ajax-select2 e

I have wriiten a code to display relevant serch results using select2 and ajax.The results are showing in console properly but not displayed in result table of select2.It just states no reults found...Below is my code:

<script type="text/javascript">

   $(".showname").select2({        
    ajax: {
        url:"http://hub.w.net/datatables/brand_processing",
        //dataType: "json",
        //delay: 250,
        type:'POST',
        data: function (params) {
            return {
                search_name: params.term // search term

               //console.log(params:params);
            };
        },
        processResults: function (data) {
            // 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
            console.log(data.data);
            return {
                 data:{text:data.data,id:data.id}
            };
        },

        cache: true
    },
    minimumInputLength: 1

}); 
</script>

Any suggestions?

Upvotes: 0

Views: 707

Answers (2)

Sajjad Ali Khan
Sajjad Ali Khan

Reputation: 1813

try this for select2 latest version

processResults: function (data, params) {

               var results = [];

                if (data != null && data.length > 0) {

                    $.each(data, function (index, item) {

                        results.push({
                            id: item.id,
                            text: item.text
                        });
                    });
                }
                return {
                    results: results
                };
};

for select2 3.5.2 version:

results: function (data, page) { // parse the results into the format expected by Select2.
                var results = [];

                if (data != null && data.length > 0) {

                    $.each(data, function (index, item) {

                        results.push({
                            id: item.id,
                            text: item.text
                        });
                    });
                }
                return {
                    results: results
                };
            },
            cache: true
        }

Upvotes: 2

try this on processResults function

return {
    text:data.data,
    id:data.id
   };

Upvotes: 0

Related Questions