Advertisement
Virajsinh

Calculate Sum (Total) of DataTables Column using Footer

Mar 4th, 2024
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.93 KB | Source Code | 0 0
  1. //Calculate Sum (Total) of DataTables Column using Footer Callback ==> https://tinyurl.com/49pe4ns5
  2.  
  3. <head>
  4. <title>Calculate Sum (Total) of DataTables Column using Footer Callback</title>
  5.     <link rel="stylesheet"  href="vendor/DataTables/datatables.min.css">   
  6.     <link rel="stylesheet"  href="style.css">  
  7.     <script src="vendor/jquery/jquery-1.11.2.min.js" type="text/javascript"></script>
  8.     <script src="vendor/DataTables/datatables.min.js" type="text/javascript"></script>  
  9. </head>
  10. <body>
  11.     <table id="tbl-attendance" class="display nowrap" cellspacing="0" width="100%">
  12.     <thead>
  13.         <tr>
  14.         <th>Subject</th>
  15.         <th>Mon</th>
  16.         <th>Tue</th>
  17.         <th>Wed</th>
  18.         <th>Thu</th>
  19.         <th>Fri</th>
  20.         </tr>
  21.     </thead>
  22.     <tfoot align="right">
  23.         <tr><th></th><th></th><th></th><th></th><th></th><th></th></tr>
  24.     </tfoot>
  25.     </table>
  26. </body>
  27.  
  28. <script>
  29. $(document).ready(function() {
  30.     $('#tbl-attendance').dataTable({
  31.         "footerCallback": function ( row, data, start, end, display ) {
  32.             var api = this.api(), data;
  33.  
  34.             // converting to interger to find total
  35.             var intVal = function ( i ) {
  36.                 return typeof i === 'string' ?
  37.                     i.replace(/[\$,]/g, '')*1 :
  38.                     typeof i === 'number' ?
  39.                         i : 0;
  40.             };
  41.  
  42.             // computing column Total of the complete result
  43.             var monTotal = api
  44.                 .column( 1 )
  45.                 .data()
  46.                 .reduce( function (a, b) {
  47.                     return intVal(a) + intVal(b);
  48.                 }, 0 );
  49.                
  50.         var tueTotal = api
  51.                 .column( 2 )
  52.                 .data()
  53.                 .reduce( function (a, b) {
  54.                     return intVal(a) + intVal(b);
  55.                 }, 0 );
  56.                
  57.             var wedTotal = api
  58.                 .column( 3 )
  59.                 .data()
  60.                 .reduce( function (a, b) {
  61.                     return intVal(a) + intVal(b);
  62.                 }, 0 );
  63.                
  64.          var thuTotal = api
  65.                 .column( 4 )
  66.                 .data()
  67.                 .reduce( function (a, b) {
  68.                     return intVal(a) + intVal(b);
  69.                 }, 0 );
  70.                
  71.          var friTotal = api
  72.                 .column( 5 )
  73.                 .data()
  74.                 .reduce( function (a, b) {
  75.                     return intVal(a) + intVal(b);
  76.                 }, 0 );
  77.            
  78.                
  79.             // Update footer by showing the total with the reference of the column index
  80.         $( api.column( 0 ).footer() ).html('Total');
  81.             $( api.column( 1 ).footer() ).html(monTotal);
  82.             $( api.column( 2 ).footer() ).html(tueTotal);
  83.             $( api.column( 3 ).footer() ).html(wedTotal);
  84.             $( api.column( 4 ).footer() ).html(thuTotal);
  85.             $( api.column( 5 ).footer() ).html(friTotal);
  86.         },
  87.         "processing": true,
  88.         "serverSide": true,
  89.         "ajax": "server.php"
  90.     } );
  91. } );
  92. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement