Edit record with dependent combo Ajax

0

I have a form with 2 dependent comboboxes: Combo A and Combo B:

When I make a new record there is no problem I select A and it shows in B what belongs to it, I select it and it registers in my DB.

My drawback is when I already want to edit the record, I load the data in that form (including Combo A with their data and it auto selects its value that is) but when I also want to load it in combo B depends on what is charged in Combo A and autoseleccionarlo the value that was passed, but it does not work

I have it like this: Article (I get the data of the record to edit)

function getArticulo(pIdArticulo){
getSeccion('cboSeccionArticulo')
$.ajax({
    url:'articulo/'+pIdArticulo+'/edit',
    type: 'GET',
    dataType:'JSON',
    success:function(data){
        getCategoriaxSeccion('cboCategoriaArticulo',data.seccion_articulo)
        $('#cboSeccionArticulo').val(data.seccion_articulo);
        $('#cboCategoriaArticulo').val(data.categoria_articulo);
    }
})}


function getSeccion(pContenedor){
$.ajax({
    url: 'seccion/',
    type: 'GET',
    dataType: 'JSON',
    success: function(response){
        $('#'+pContenedor).empty()
        $('#'+pContenedor).append('<option value="">'+'[ Selecciona ]'+'</option>')
        for (var i =0; i<response.length; i++) {
            $('#'+pContenedor).append('<option value="'+response[i].id_seccion+'">'+response[i].detalle_seccion+'</option>')
        }
    }
})}

function getSeccion(pContenedor){
$.ajax({
    url: 'seccion/',
    type: 'GET',
    dataType: 'JSON',
    success: function(response){
        $('#'+pContenedor).empty()
        $('#'+pContenedor).append('<option value="">'+'[ Selecciona ]'+'</option>')
        for (var i =0; i<response.length; i++) {
            $('#'+pContenedor).append('<option value="'+response[i].id_seccion+'">'+response[i].detalle_seccion+'</option>')
        }
    }
})

}

I understand that the call is asynchronous, but does anyone know any way to do it or is there some property that I am not considering?

    
asked by frijjolitto 16.06.2017 в 05:08
source

1 answer

1

In the formula of your ComboB send the selected value, so that when returning from your ajax and you have correctly loaded the combo, you can set the selection, that is, your function should be more or less like this:

function getSeccion(pContenedor, valorSeleccionado){
$.ajax({
    url: 'seccion/',
    type: 'GET',
    dataType: 'JSON',
    success: function(response){
        $('#'+pContenedor).empty()
        $('#'+pContenedor).append('<option value="">'+'[ Selecciona ]'+'</option>')
        for (var i =0; i<response.length; i++) {
            $('#'+pContenedor).append('<option value="'+response[i].id_seccion+'">'+response[i].detalle_seccion+'</option>')
        }

if (valorSeleccionado)
        $('#'+pContenedor).val(valorSeleccionado);
    }
})

The issue is that when you call $ ('# cboCategoriaArticulo'). val (data.categoria_articulo); Your combo is still not created because it is async.

    
answered by 16.06.2017 в 06:55