V
V
Viktor Yanyshev2018-02-11 14:12:12
JavaScript
Viktor Yanyshev, 2018-02-11 14:12:12

Why doesn't select2 make the array look like it needs to work?

As in the dock for select2 it is written that the result from the backend must be brought to the required form [{id:1, text: 'text'},{id:2,text:'text'}]. In order not to twist the result array into the desired view on the back, you can use $.map. But, the answer from the back still does not display:
5a8025027e9be209503299.png5a80250b0411d528495911.png

processResult: function(data) {
             var tags = $.map(data.results, function(obj){
                obj.id = ob.id;
                obj.text = obj.name;
                return obj;
             });

            return {
                results: tags
            }

Full code
$('select#forum-tags_id').select2({
    ajax: {
        url: location.pathname,
        type: 'post',
        dataType: 'json',
        data: function(params) {
         var query = {
            search: params.term
         };
         
         return query;
        },
        processResult: function(data) {
             var tags = $.map(data.results, function(obj){
                obj.id = ob.id;
                obj.text = obj.name;
                return obj;
             });

            return {
                results: tags
            }
        },
    },
    placeholder: 'Начните вводить название тега'
 });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-02-11
@Rsa97

What errors does it show in the console? Where did the ob variable come from?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question