获取从jquery ajax dataTable

时间:2016-09-20 08:00:48

标签: jquery datatable

我有一个如下定义的dataTable。

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
    } );

如果数据不存在,我得到的响应是这样的。

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

我需要检查响应,即如果iTotalRecords=0,我需要显示禁用下载按钮,否则启用它。

我使用了以下代码

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

没用。请帮帮我

3 个答案:

答案 0 :(得分:2)

你必须更换" sAjaxSource"有一个自定义的,见下文。

$('#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; 
                    }
         },

})

答案 1 :(得分:1)

改为使用它。

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; 
                                       }
                        },

        });

答案 2 :(得分:0)

首先从服务器获取数据然后初始化为jqu​​ery 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
        }
    });