Thursday, March 1, 2018

jQuery Datatables - footerCallback sum columns, issues with table total

Leave a Comment

ive created a function that I can call on columns that I wish to sum up using the below. however the total (table total) entry for column 9 is always zero. the page total seems to work. and the page total and table total for column 9 works also.

"footerCallback": function ( row, data, start, end, display ) {             var api = this.api(), data;             // Remove the formatting to get integer data for summation             var intVal = function ( i ) {                 return typeof i === 'string' ?                     i.replace(/[\£,]/g, '')*1 :                     typeof i === 'number' ?                         i : 0;             };             var column_sum = function (col) {                 // Total over all pages                 total = api                     .column(col)                     .data()                     .reduce( function (a, b) {                         return intVal(a) + intVal(b);                     }, 0 );                 // Total over this page                 pageTotal = api                     .column(col, { page: 'current'} )                     .data()                     .reduce( function (a, b) {                         return intVal(a) + intVal(b);                     }, 0 );                 return accounting.formatMoney(pageTotal) +' ('+ accounting.formatMoney(total) +' total)'             };             // Update footer             $( api.column(6).footer()).html(                 column_sum(6)             );             $( api.column(9).footer()).html(                 column_sum(9)             );         } 

EDIT

I have added some sanitised data. currently the page total works for the first column and second columns.

The all pages total does not work. i.e each time I filter by the column header I should see the total across each page

<table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer dtr-inline" id="circuit_list"  role="grid" style="width: 100%;">     <thead>         <tr>             <th>Info</th>             <th>Type</th>             <th>Cost PM</th>             <th>Term</th>             <th>Remaining Term</th>             <th>Remaining Cost</th>         </tr>     </thead>     <tbody>         <tr>             <td>                 <a href="/circuits/edit/238/1/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>Fibre</td>             <td>£950.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/238/2/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>Fibre</td>             <td>£950.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/333/101/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£1791.33</td>             <td>12</td>             <td>11</td>             <td>£19,704.63</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/334/101/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£100.00</td>             <td>12</td>             <td>11</td>             <td>£1,100.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/235/1/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£593.33</td>             <td>36</td>             <td>15</td>             <td>£8,899.95</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/317/82/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>Fibre</td>             <td>£103.00</td>             <td>3</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/229/2/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£373.33</td>             <td>36</td>             <td>11</td>             <td>£4,106.63</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/233/1/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>DSL</td>             <td>£1837.66</td>             <td>60</td>             <td>6</td>             <td>£11,025.96</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/234/1/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>DSL</td>             <td>£373.34</td>             <td>36</td>             <td>15</td>             <td>£5,600.10</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/243/5/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>DSL</td>             <td>£373.34</td>             <td>36</td>             <td>15</td>             <td>£5,600.10</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/244/4/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£373.34</td>             <td>36</td>             <td>12</td>             <td>£4,480.08</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/324/83/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£103.00</td>             <td>3</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/2/6/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£41.50</td>             <td>12</td>             <td>0</td>             <td>0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/57/18/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£45.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/113/35/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>Fibre</td>             <td>£45.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/218/71/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£57.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/264/71/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>MPLS</td>             <td>£45.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/269/61/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>DSL</td>             <td>£45.00</td>             <td>12</td>             <td>0</td>             <td>£0.00</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/300/85/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£30.00</td>             <td>12</td>             <td>&nbsp;</td>             <td>&nbsp;</td>         </tr>         <tr>             <td>                 <a href="/circuits/edit/307/76/all_cl" class="btn btn-primary btn-circle">                     <i class="fa fa-list"></i>                 </a>             </td>             <td>4G</td>             <td>£45.00</td>             <td>12</td>             <td>6</td>             <td>270.00</td>         </tr>     </tbody> </table> 

1 Answers

Answers 1

Most probably in one or more of the cells for column 9, the value there fails to convert to a number and you are getting a NaN returned by intVal function.

There are 2 issues in intVal:

  1. line i.replace(/[\£,]/g, '')*1 can return NaN
  2. typeof i === 'number' can be true for NaN

From what it seems, intVal is trying to check for this, but the logic is wrong. change the intVal to:

var intVal = function ( i ) {     if(typeof i === 'string') {          i = i.replace(/[\£,]/g, '')*1;    }    // check if you got a valid number.    if (Number.isNaN(i)) {          return 0;    }    return i; }; 

If the filtered total is needed, use:

total = api.column(col, {"filter": "applied"})  

or

total = api.column(col, {"search": "applied"}) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment