Delay in returning the data to my select with Ajax - jQuery

1

I have a small problem when implementing the edit button it brings me all the data in a fast way in my inpust, but my select is delayed.

obtener_unidad_registro();// Son las funciones donde lleno mi combo
obtener_categoria_registro();// Son las funciones donde lleno mi combo

This is my Jquery with my Ajax :

function  modificar_producto(codigo) {

    bordes_limpias();
    obtener_unidad_registro();
    obtener_categoria_registro();

    $.ajax({
        url: baseurl + 'Producto/obtener_producto',
        type: "POST",
        data: {codigo: codigo},
        dataType: "JSON",
        success: function (data)
        {
            $('#codigo_producto').prop('disabled', true);
            $('#nombre_producto').prop('disabled', true);
            $('#codigo_producto').val(data[0]['codigo_producto']);
            $('#nombre_producto').val(data[0]['nombre']);
            $('#precio_entrada').val(data[0]['precio_entrada']);
            $('#precio_salida').val(data[0]['precio_salida']);
            $('#stock_actual').val(data[0]['stock_actual']);
            $('#stock_minimo').val(data[0]['stock_minimo']);
            $("#modal_form_producto").unbind();
            $('#modal_form_producto').modal('show')
            var unidad = data[0]['unidad'];
            var categoria = data[0]['categoria'];
            $("#cb_categoria option[value='" + categoria + "']").attr('selected', 'selected');
            $("#cb_unidad option[value='" + unidad + "']").attr('selected', 'selected');
            $('#boton_multiuso').attr("onclick", 'actualizar_producto(' + codigo + ')');
            $('#msg_cabecera').html("EDITAR PRODUCTO #" + codigo);
        }
    });
}

THE FUNCTIONS OF THE COMBOS

function obtener_unidad_registro() {
    $.ajax({
        url: baseurl + 'Producto/obtener_unidad',
        type: "POST",
        dataType: "JSON",
        success: function (data)
        {
            $('#cb_unidad').empty();
            $('#cb_unidad').prop('disabled', false);
            $('#cb_unidad').append(data);
        }
    });
}

function obtener_categoria_registro() {
    $.ajax({
        url: baseurl + 'Producto/obtener_categoria',
        type: "POST",
        dataType: "JSON",
        success: function (data)
        {
            $('#cb_categoria').empty();
            $('#cb_categoria').prop('disabled', false);
            $('#cb_categoria').append(data);
        }
    });
}

CONTROLLER

public function obtener_unidad() {
        $niveles = $this->producto->obtener_unidad();
        $resp = '<option name="" value="">--SELECCIONE UNIDAD--</option>';
        foreach ($niveles as $row) {
            $resp .= '<option name="' . $row ['id_unidad'] . '"  value="' . $row ['nombre'] . '">' . $row ['nombre'] . '</option>';
        }
        echo json_encode($resp);
    }

    public function obtener_categoria() {
        $niveles = $this->producto->obtener_categoria();
        $resp = '<option name="" value="">--SELECCIONE CATEGORIA--</option>';
        foreach ($niveles as $row) {
            $resp .= '<option name="' . $row ['id_categoria'] . '" value="' . $row ['nombre'] . '">' . $row ['nombre'] . '</option>';
        }
        echo json_encode($resp);
    }

MODEL

public function obtener_unidad() {
        $query = $this->db_venta->query("SELECT * FROM unidad");
        return $query->result_array();
    }

    public function obtener_categoria() {
        $query = $this->db_venta->query("SELECT * FROM categoria");
        return $query->result_array();
    }

I come into view and I'm going to edit an 'X' product and I get this.

After several attempts I just got this

    
asked by Joel More F. 23.06.2017 в 22:14
source

1 answer

0

What you do in the model I do in the example controller and I add the data dynamically but in the js.

CONTROLLER

    public function obtener_unidad() {
                $resp = $this->db_venta->query("SELECT * FROM categoria");
    //si tiene creado el modelo de categoria es mucho mas facil
   // $resp =Categoria::all();
                 echo json_encode($resp);
                }

now in the java answer script

function obtener_categoria_registro() {
    $.ajax({
        url: baseurl + 'Producto/obtener_categoria',
        type: "POST",
        dataType: "JSON",
        success: function (data)
        {
              var m=data.length;
           for(var t=0; t<m; t=t+1) {
                            var datos = data[t];

$('#cb_categoria').append('<option name="' + data['id_categoria'] + '" value="' + data['nombre'] + '">' +data['nombre'] + '</option>');

        }
    });
} 
    
answered by 23.06.2017 в 23:02