Datatables adding JSON data to the <tfoot> footer

I am using datatables to display some JSON data and I have included the request totals at the bottom of the JSON like so:

{
    "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
}

      

I am a little confused as to how I can add them to the footer of my table as before that I had access to the php variables directive and now I encode them to JSON. If anyone has experience with this and using footerCallback

datatables in initialization, that would be really great.

Thank you in advance

+3


source to share


1 answer


You can use data from JSON response in DataTables footer as shown below.



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

      

+6


source







All Articles