Select2: Does not show results, but if you search

0

the last days I'm trying this topic I try to use the select2 I do the search but it does not show the results, apart from the select2 I have 2 inputs that are autopleted with the information that brings ajax , I hope to find a solution to this problem.

Thanks.

HTML CODE

<div class="col-md-6">
          <div class="form-group">
              <label class="control-label col-md-4">Producto</label>
                <div class="col-md-12">
                  <select class="form-control select2" id="producto" 
                   style="width: 100%">
                  </select> <span class="help-block"></span>
                 </div>
           </div>
</div>

JQUERY

 $("#producto").select2({
        placeholder: "Buscar producto",
        allowClear: true,
        minimumInputLength: 1,
        ajax: {
            url: baseurl + 'Venta/obtener_producto',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: data
                };
            },
            cache: true
        },
    });

    
asked by Ivan More Flores 12.06.2017 в 21:37
source

1 answer

1

You should change the processResults as follows:

processResults: function(data) {
    return {
        results: $.map(data, function(obj) {
            return {
                id: obj.id,
                text: obj.name
            };
        })
    };
}

Generating a new array with two properties (id, text) that is what select2 expects.

Greetings

    
answered by 13.06.2017 / 15:31
source