Skip to content
Advertisement

jQuery datatables ajax callback

I am using jQuery DataTables and doing server-side data. I’m trying to call a function when the ajax call returns. I tried inserting this fnCallback2 which calls my function and the original function, but jQuery just throws an error (and doesn’t tell me what the error is) and skips out.

$("#brands").dataTable( {
"bServerSide" : true,
"sAjaxSource" : "ajax.php",
"fnServerData" : function(sSource, aoData, fnCallback) {
    fnCallback2 = function(a,b,c){
        fnCallback.call(a,b,c);
        update_editable();
    };
    $.ajax( {
        "dataType" : 'json',
        "type" : "POST",
        "url" : sSource,
        "data" : aoData,
        "success" : fnCallback2
    });}});

I also tried adding the fnInitComplete parameter, but that only gets called the first time, not after subsequent pages.

"fnInitComplete": function(){
update_editable();
},

How do I correctly call my code after the ajax request so that the original callback gets called as well?

Advertisement

Answer

Another option is to use the fnDrawCallback that is called after each draw event. Which will be done after every ajax request.

"fnDrawCallback" : function() {
    update_editable();
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement