Techy
Techy

Reputation: 2654

get the data retrieved from jquery ajax dataTable

I have a dataTable defined like the following.

oTable = $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": source_url,
    "aoColumns": [
            null,
            null,
            null,
            null,
            //{ "sClass": "left", "bSortable": false },
            null
        ],                                                          
        "aaSorting": [[4, 'desc']],
        "fnDrawCallback": fnOpenClose
    } );

If datas are not there,I am getting the response is like this.

{"sEcho": 1, "iTotalRecords": 0, "iTotalDisplayRecords": 0, "aaData": [] }

I need to check the response,ie ifiTotalRecords=0,I need to show a disable download button,else enable it.

I have used the following code

"fnInitComplete": function(oSettings, json) {
  alert( 'DataTables has finished its initialisation.' );
}

It was not working.Please help me

Upvotes: 0

Views: 2811

Answers (3)

Emz
Emz

Reputation: 542

Use this instead.

var mainTable = $('#Table').DataTable({

            "processing": true,
            "serverSide": true,
            "columns"   : YOUR_COLUMN_DEF_HERE,
            "ajax"      : {
                            "url"    : YourServerSideDataSrc,
                            "type"   : "POST",
                            "dataSrc": function (response) { 
                                        if(response.whaterver == 0){
                                           //DO YOUR THING HERE
                                        }
                                        //return back the response
                                        return response; 
                                       }
                        },

        });

Upvotes: 2

User
User

Reputation: 1363

first you get data from server then initialize to jquery dataTable

   $.ajax({
        type: "POST",
        url: "your url",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        datatype: "jsondata",
        async: "true",
        success: function(response) {
         if(response.d.length!=0)           
            {
             var dataSet = [];
              for (var i = 0; i <= response.d.length - 1; i++) {             

                dataSet[i] = [];
                //set data in dataSet                 
              }                             
            $('#tblreportmaster').html('<table class="table table-striped table-bordered table-hover" id="tblreport"></table>');
            $('#tblreport').dataTable({
                "data": dataSet,
                "columns": [ put column name Here ],                                        
            "scrollY": "400px",
            "scrollCollapse": true,
            "paging": false
            });
         }
        },
        error: function(response) {
           //error
        }
    });

Upvotes: 0

Emz
Emz

Reputation: 542

You have to replace "sAjaxSource" with a custom one see below.

$('#table').datatable({

"ajax" : {
         "url" : "?yourServerSideDataSource",
         "type" : "POST",
         "dataSrc": function (response) { 
                       if ( response.iTotalRecords == 0 ) {
                          //DO YOUR THING HERE
                       }
                       //You have to return back the response
                       return response; 
                    }
         },

})

Upvotes: 2

Related Questions