Change the value of the select2 with JQuery [duplicated]

0

HELP

Hello, I need to know how I can change the value of a .select2 .

That is, when I give it to the edit button, I have to fill in all fields filled in all the fields except the select2

WHEN WHEN I DECIDE YOU TO EDIT ME AUTORRELLENE .SELECT WITH THE OPTION TO BE KEPT

HTML CODE

<div class="col-md-6">
      <div class="form-group">
        <label class="control-label col-md-6">Proveedor</label>
          <input id="id_proveedor" type="hidden">
            <div class="col-md-12">
              <select class="form-control select2" id="nombre_proveedor"
                                                    style="width: 100%"></select> <span class="help-block"></span>
            </div>
          </div>

MY JQUERY

function modificar_compra(id_compra) {
        combo_select2();
    $.ajax({
        url: baseurl + 'Compras/obtener_compra',
        type: "POST",
        data: {id_compra: id_compra},
        dataType: "JSON",
        success: function (data)
        {
            $("#modal_form_compra").unbind();
            $('#modal_form_compra').modal('show')
            $("#div_imagen_subir").hide();
            $("#div_imagen").show();
            $('#boton_multiuso').attr("onclick", 'actualizar_compra(' + id_compra + ')');
            $('#msg_cabecera').html("EDITAR COMPRA #" + id_compra);
            $('#numero_compra').val(data[0]['numero_recibo']);
            $('#id_proveedor').val(data[0]['id_proveedor']);
            $('#monto_total').val(data[0]['monto_total']);
            $('#fecha_compra').val(data[0]['fecha']);
            var proveedor = data[0]['id_proveedor'];
            console.log(proveedor);
//            $('#nombre_proveedor').select2();
            $('#nombre_proveedor').val(proveedor).trigger('change.select2');
            $('#id_imagen_editar').attr("src", baseurl + 'dist/img/compras/' + data[0]['url_imagen']);
        }
    });
}
function  combo_select2() {
    $("#nombre_proveedor").select2({
        theme: "bootstrap",
        placeholder: "Buscar proveedor",
        allowClear: true,
        minimumInputLength: 1,
        ajax: {
            url: baseurl + 'Compras/obtener_proveedor',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (obj) {
                        return {
                            id: obj.id_proveedor,
                            text: obj.nombre_proveedor
                        };
                    })
                };
            },
            cache: true
        }
    });
}
    
asked by Joel More F. 21.06.2017 в 21:34
source

2 answers

0
function  combo_select2() {
    $("#nombre_proveedor").select2("destroy");
    $("#nombre_proveedor").select2({
        theme: "bootstrap",
        placeholder: "Buscar proveedor",
        allowClear: true,
        minimumInputLength: 1,
        ajax: {
            url: baseurl + 'Compras/obtener_proveedor',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (obj) {
                        return {
                            id: obj.id_proveedor,
                            text: obj.nombre_proveedor
                        };
                    })
                };
            },
            cache: true
        }
    });
}
    
answered by 21.06.2017 в 22:10
0

Add destroy between the properties of select2 , this is something similar to declare it as $("#nombre_proveedor").select2("destroy") , but since it is not yet initialized it tends to drop error, where it is better to leave it as follows:

function  combo_select2() {
    $("#nombre_proveedor").select2({
        destroy: true,//primero destruye el anterior y después lo vuelve a cargar
        theme: "bootstrap",
        placeholder: "Buscar proveedor",
        allowClear: true,
        minimumInputLength: 1,
        ajax: {
            url: baseurl + 'Compras/obtener_proveedor',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (obj) {
                        return {
                            id: obj.id_proveedor,
                            text: obj.nombre_proveedor
                        };
                    })
                };
            },
            cache: true
        }
    });
}
    
answered by 21.06.2017 в 22:58