Skip to content

Instantly share code, notes, and snippets.

@TobiasBg
Last active August 29, 2015 14:13
Show Gist options
  • Save TobiasBg/3061f477c6d0745ea467 to your computer and use it in GitHub Desktop.
Save TobiasBg/3061f477c6d0745ea467 to your computer and use it in GitHub Desktop.
DataTables footerCallback Extension
<?php
/*
Plugin Name: TablePress Extension: Custom "footerCallback"
Plugin URI: http://tablepress.org/extensions/
Description: Custom Extension for TablePress to add a "footerCallback"
Version: 1.0
Author: Tobias Bäthge
Author URI: http://tobias.baethge.com/
*/
add_filter( 'tablepress_datatables_parameters', 'tablepress_datatables_custom_footercallback', 10, 4 );
/**
* Add a custom "footerCallback".
*
* @since 1.0.0
*
* @param array $parameters DataTables parameters.
* @param string $table_id Table ID.
* @param string $html_id HTML ID of the table.
* @param array $js_options JS options for DataTables.
* @return array Extended DataTables parameters.
*/
function tablepress_datatables_custom_footercallback( $parameters, $table_id, $html_id, $js_options ) {
if ( in_array( $table_id, array( '1', '4 ') ) ) {
$parameters['footerCallback'] = <<<JS
"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);
} );
// Total over this page
pageTotal = api
.column( 4, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
JS;
}
return $parameters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment