Return a variable that is within an ajax [duplicated]

2

I am looking for a way to show a child.rows but the only way that can be shown is with a return direct. then I made this code so that outside of ajax I take those values but it does not give me, I hope you can help me.
CODE

        function format (data) {
         $.ajax({
           url: "list_data",
           type: 'POST', 
           data: {'data':data[0]}, 
           success: function (result){
             for (var i = result.length - 1; i >= 0; i--){ 
               var resultado =  '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
                '<tr>'+
                  '<td>Full name:result[i].nombre</td>'+
                  '<td></td>'+
                '</tr>'+
               '</table>';
             }
             return resultado;
           }
         });
        }


return result; this is what I think is wrong, and as you see I need to return that code inside the for and if I do the return inside the for does not work it has to be outside the ajax method

$('#example tbody').on('click', 'td.details-control', function () {
    var tr = $(this).closest('tr');
    var row = table.row( tr );

    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child( format(row.data()) ).show();
        tr.addClass('shown');
    }
} );
    
asked by JDavid 20.01.2017 в 21:31
source

1 answer

3

Because of its asynchronous nature, you can not make your function return a value from success of ajax ; you have to handle it within success .

You can do it, for example, in the following way:

// Se agrega el parámetro "callback", que debe ser una función
function format (data, callback) {
    $.ajax({
        url: "list_data",
        type: 'POST', 
        data: {'data':data[0]}, 
        success: function (result){
            for (var i = result.length - 1; i >= 0; i--){ 
                var resultado =  '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
                    '<tr>'+
                    '<td>Full name:result[i].nombre</td>'+
                    '<td></td>'+
                    '</tr>'+
                    '</table>';
            }
            // Tras obtener tu resultado, ejecutas la función recibida
            // como parámetro, con "resultado" como argumento
            callback(resultado);
        }
    });
}

And in your main code, you call your function format like this:

$('#example tbody').on('click', 'td.details-control', function () {
    var tr = $(this).closest('tr');
    var row = table.row( tr );

    if ( row.child.isShown() ) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        format(row.data(), function(resultado){
            // "resultado" contiene la cadena que necesitas retornar
            row.child(resultado).show();
            tr.addClass('shown');
        });
    }
} );
    
answered by 20.01.2017 / 22:15
source