This question is based on footerCallback with duplicate cell numbers
I implement the solution from @andrewJames (thanks).
I'm trying to sum last column of the table exept the salary of System Architect
and Senior Javascript Developer
But now there is a problem when I use JSON data. The sum
is always 0
... instead of $9
HTML:
<div class="row">
<div class="large-12 columns">
<table id="example" class="display nowrap table1" cellspacing="0" width="100%">
<thead>
<tr>
<th>Seq.</th>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Seq.</th>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
</div>
</div>
jQuery:
var salaryTable = {
"data": [
{
"Seq": "1",
"Name": "Tiger Nixon",
"Position": "System Architect",
"Salary": "$1"
},
{
"Seq": "1",
"Name": "Garrett Winters",
"Position": "Accountant",
"Salary": "$1"
},
{
"Seq": "3",
"Name": "Ashton Cox",
"Position": "Junior Technical Author",
"Salary": "$3"
},
{
"Seq": "4",
"Name": "Cedric Kelly",
"Position": "Senior Javascript Developer",
"Salary": "$4"
},
{
"Seq": "5",
"Name": "Airi Satou",
"Position": "Accountant",
"Salary": "$5"
}
]
};
$(document).ready(function() {
var table = $('#example').DataTable( {
rowReorder: {
selector: 'td:nth-child(2)'
},
data: salaryTable.data,
columns: [
{ data: "Seq" },
{ data: "Name" },
{ data: "Position" },
{ data: "Salary" }
],
responsive: true,
scrollX: true,
scrollY: "80vh",
scrollCollapse: true,
paging: true,
lengthChange: false,
lengthMenu: [ [10, 25, -1], [10, 25, "All"] ],
"order": [[ 0, "asc" ]],
"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;
};
filteredTotal = api.rows().data().reduce(function (a, b) {
// filterMe will be true or false:
filterMe = b[2] === "System Architect" || b[2] === "Senior Javascript Developer";
// if filterMe is true then use 0, otherwise use the actual amount from b[3]:
salary = filterMe ? 0 : intVal(b[3]);
return a + salary;
}, 0 );
// Total over this page
pageTotal = api
.column( 3, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 3 ).footer() ).html(
'$'+filteredTotal+'/ all $'+pageTotal
);
},
buttons: ['pdf', 'print']
} );
table.buttons().container()
.appendTo( '#example_wrapper .small-6.columns:eq(0)' );
} );
$(document).foundation();
Fiddle with the problem: https://jsfiddle.net/87360vyx/1/