Uder Moreira
Uder Moreira

Reputation: 932

fnOpen on DataTables are not waiting $.ajax request

I have a code that I'm trying to make work whith jQuery

$('#example tbody td img').live('click', function () {
    var nTr = $(this).parents('tr')[0];
    if (oTable.fnIsOpen(nTr)) { /* This row is already open - close it */
        this.src = "../compartilhados/img/details_open.png";
        oTable.fnClose(nTr);
    } else { /* Open this row */
        this.src = "../compartilhados/img/details_close.png";
        oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details ui-corner-all' );
    }
});

the function fnFormatDetails used to return a string, but I modified it to return the response of a $.ajax:

    function fnFormatDetails(oTable, nTr) {
        var aData = oTable.fnGetData(nTr);
        var parametros = {
            NumPA: aData[8]
        };
        var parametros = jQuery.param(parametros);
        $.ajax({
            type: "POST",
            url: "consultarProvidencias.asp",
            data: parametros
        }).done(function x(sOut) {
        return sOut;
        });
    }

and sOut is a peace of html code, like a string. I don't know what are happening, cause sOut is not been loading on oTable.fnOpen (first code). It must be load on details of row, like it is shown here.

I appreciated any help.

Upvotes: 0

Views: 2699

Answers (1)

thitemple
thitemple

Reputation: 6059

You have to change your function fnFormatDetails to return the response of the ajax call, like this:

function fnFormatDetails(oTable, nTr) {
        var aData = oTable.fnGetData(nTr);
        var parametros = {
            NumPA: aData[8]
        };
        var parametros = jQuery.param(parametros);
        return $.ajax({
            type: "POST",
            url: "consultarProvidencias.asp",
            data: parametros
        });
    }

After that you change the way you're calling that function:

var data = fnFormatDetails(oTable, nTr);
$.when(data).then(function(theData) {
  oTable.fnOpen(nTr, theData, 'details ui-corner-all' );
});

Upvotes: 3

Related Questions