Individual column searching and sum
Individual column searching and sum
chrkon
Posts: 1Questions: 1Answers: 0
Hello all,
I use the code below to search my columns individually and it works fine
$(document).ready(function() {
// Setup - add a text input to each header cell
$('#example thead tr').clone(true).appendTo( '#example thead' );
$('#example thead tr:eq(1) th').each( function (i) {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search.." />' );
$( 'input', this ).on( 'keyup change', function () {
if ( table.column(i).search() !== this.value ) {
table
.column(i)
.search( this.value )
.draw();
}
} );
} );
var table = $('#example').DataTable( {
orderCellsTop: true,
fixedHeader: true
} );
} );
The case here is that i need to sum a column on the footer using the following code but i don't seem to find a way to combine the two.
$(document).ready(function() {
$('#example').DataTable( {
"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;
};
// Total over all pages
total = api
.column( 4 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 4, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total filtered rows on the selected column (code part added)
var sumCol4Filtered = display.map(el => data[el][4]).reduce((a, b) => intVal(a) + intVal(b), 0 );
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total) ($' + sumCol4Filtered +' filtered)'
);
}
} );
} );
The footercallback code works also fine by itself...
Any suggestions?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
I'm guessing the problem is that you are getting a syntax error when trying to use both?
If so you need to add a comma to separate the options, see below:
Kevin