tigrang / cakephp-datatable

JQuery DataTable plugin server-side processing component for CakePHP
47 stars 29 forks source link

Way to specify callbacks #42

Closed tranfuga25s closed 10 years ago

tranfuga25s commented 10 years ago

is there a way to specify some callback and not get parsed as string to get something like this to the output:

$('#example').dataTable( {
        "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
            /* Modify the footer row to match what we want */
            var nCells = nRow.getElementsByTagName('th');
            nCells[1].innerHTML = parseInt(iPageMarket * 100)/100 +
                '% ('+ parseInt(iTotalMarket * 100)/100 +'% total)';
        }
} );

if specified as "fnFooterCallback" => "function()..." it get's wrapped in text and never get excuted..

tigrang commented 10 years ago

PHP's json_encode doesn't support functions. So your option is provide your own init method, and add your function to the dataTableSettings js var that is set with the helper.

Set scriptBlock to false for the helper settings so it doesn't add the default to the scripts block, and write your own init method, like so:

$('.dataTable').each(function() {
    var table = $(this);
    var model = table.attr('data-model');
    var settings = dataTableSettings[model];
    settings['fnFooterCallback'] = function() { ... }; // This line, adds it to the rest of the settings the helper generates
    table.dataTable(settings);
});