call a function in success of datatable ajax call

后端 未结 10 1949
难免孤独
难免孤独 2020-12-29 01:44

Is that possible to invoke a javascript function in success of datatable ajax call. Here is the code am trying to use,

var oTable = $(\'#app-config\').dataTa         


        
相关标签:
10条回答
  • 2020-12-29 02:17

    You can use this:

    "drawCallback": function(settings) {
       console.log(settings.json);
       //do whatever  
    },
    
    0 讨论(0)
  • 2020-12-29 02:17

    Maybe it's not exactly what you want to do, but using the ajax complete solved my problem of hiding a spinner when the ajax call returned.

    So it would look something like this

    var table = $('#example').DataTable( {
        "ajax": {
                "type" : "GET",
                "url" : "ajax.php",
                "dataSrc": "",
                "success": function () {
                    alert("Done!");
                }       
        },
        "columns": [
                { "data": "name" },
                { "data": "position" },
                { "data": "office" },
                { "data": "extn" },
                { "data": "start_date" },
                { "data": "salary" }            
            ]
        } );
    
    0 讨论(0)
  • 2020-12-29 02:19

    The best way I have found is to use the initComplete method as it fires after the data has been retrieved and renders the table. NOTE this only fires once though.

    $("#tableOfData").DataTable({
            "pageLength": 50,
            "ajax":{
                url: someurl,
                dataType : "json",
                type: "post",
                "data": {data to be sent}
            },
            "initComplete":function( settings, json){
                console.log(json);
                // call your function here
            }
        });
    
    0 讨论(0)
  • 2020-12-29 02:22

    Try Following Code.

           var oTable = $('#app-config').dataTable(
            {
                "bAutoWidth": false,                                                
                "bDestroy":true,
                "bProcessing" : true,
                "bServerSide" : true,
                "sPaginationType" : "full_numbers",
                "sAjaxSource" : url,                    
                "fnServerData" : function(sSource, aoData, fnCallback) {
                    alert("sSource"+ sSource);
                    alert("aoData"+ aoData);
                    $.ajax({
                        "dataType" : 'json',
                        "type" : "GET",
                        "url" : sSource,
                        "data" : aoData,
                        "success" : fnCallback
                    }).success( function(){  alert("This Function will execute after data table loaded");   });
                }
    
    0 讨论(0)
  • 2020-12-29 02:25

    The success option of ajax should not be altered because DataTables uses it internally to execute the table draw when the data load is complete. The recommendation is used "dataSrc" to alter the received data.

    0 讨论(0)
  • 2020-12-29 02:25

    Based on the docs, xhr Ajax event would fire when an Ajax request is completed. So you can do something like this:

    let data_table = $('#example-table').dataTable({
            ajax: "data.json"
        });
    
    data_table.on('xhr.dt', function ( e, settings, json, xhr ) {
            // Do some staff here...
            $('#status').html( json.status );
        } )
    
    
    0 讨论(0)
提交回复
热议问题