数据表将JSON数据添加到页脚

时间:2015-04-19 02:28:13

标签: jquery json jquery-datatables

我正在使用数据表以JSON格式显示一些数据,并且我在JSON的底部包含了查询总数,如下所示:

{
    "data": [
        {
            "id": "1",
            "provider_num": "381301",
            "provider_name": "COTTAGE GROVE COMMUNITY HOSPITAL",
            "261_total_bad_debts": "$0",
            "271_medicare_bad_debts": "$79,275",
            "281_non_medicare_bad_debts": "$-79,275",
            "1_cost_to_charge_ratio": "0.703459",
            "291_cost_of_non_mcr_bad_debts": "$-55,767"
        }
    ],
    "total_bad_debts": 0,
    "total_medicare_bad_debts": 79275,
    "total_non_medicare_bad_debts": -79275,
    "total_cost_of_non_mcr_bad_debts": -55767
}

我对如何将它们添加到我的表的页脚有点困惑,因为之前我已经访问了php变量,现在我在JSON中对它们进行编码。如果有人对此有任何经验并在数据表初始化中使用footerCallback,那将非常棒。

提前致谢

2 个答案:

答案 0 :(得分:8)

您可以使用DataTables页脚中的JSON响应数据,如下所示。

$('#example').dataTable( {
   'ajax': 'data/arrays.txt',
   'footerCallback': function( tfoot, data, start, end, display ) {    
      var response = this.api().ajax.json();
      if(response){
         var $th = $(tfoot).find('th');
         $th.eq(0).html(response['total_bad_debts']);
         $th.eq(1).html(response['total_medicare_bad_debts']);
         $th.eq(2).html(response['total_non_medicare_bad_debts']);
         $th.eq(3).html(response['total_cost_of_non_mcr_bad_debts']);
      }
   } 
});

答案 1 :(得分:0)

$('#example').dataTable( {
       'ajax': 'data/arrays.txt',
       'footerCallback': function( row, data, start, end, display ) {    

            var api = this.api();
            var response = this.api().ajax.json();
            if (response) {

                $(api.column(3).footer()).html(
                    response.total_bad_debts
                );

                $(api.column(4).footer()).html(
                    response.total_medicare_bad_debts
                );

                $(api.column(5).footer()).html(
                    response.total_non_medicare_bad_debts
                );

                $(api.column(6).footer()).html(
                    response.total_cost_of_non_mcr_bad_debts
                );
            }
        } 
    });