Server-side: formatter function

Server-side: formatter function

CountyCounty Posts: 10Questions: 4Answers: 0
edited May 2021 in DataTables 1.10

Hello all,
The goal of my current script is to get server-side processing working with "custom" variables, i.e., variables that I need to modify before being passed to the table.
For example, in the code below, I am trying to convert a value in milliseconds into minutes:seconds:milliseconds in the server side script. Does anyone know how I can use more than two variables in the "return"?

Currently, the codereturn number_format($minutes, $seconds, $milliseconds); does not return any value while return number_format($minutes, $seconds); does work. I assume it may have something to do with 'formatter' => function( $d, $row) only passing two variables instead of the three I need?

The full script is below. Thank you for your assistance!

$columns = array(
    array( 'db' => 'ID', 'dt' => 0 ),
    array(
        'db'        => 'alternateEventDurationInMilliseconds',
        'dt'        => 1,
        'formatter' => function( $d, $row) {
            
$milliseconds = $d;
$time = $milliseconds / 1000;
$days = floor($time / (24*60*60));
$hours = floor(($time - ($days*24*60*60)) / (60*60));
$minutes = floor(($time - ($days*24*60*60)-($hours*60*60)) / 60);![](https://datatables.net/forums/uploads/editor/qk/286qsd8vyhna.jpg "")

$seconds = ($time - ($days*24*60*60) - ($hours*60*60) - ($minutes*60)) % 60;


            return number_format($minutes, $seconds, $milliseconds);
        }

),

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,686Questions: 1Answers: 10,500 Site admin

    The PHP documentation for number_format says that the third third parameter passed into that function is the decimal character. You appear to be passing a number into it?

    Do you actually just need?:

    return $minutes .’:’. $seconds .’:’. $milliseconds;
    

    Allan

  • CountyCounty Posts: 10Questions: 4Answers: 0

    You are correct about that, Allan. I've adjusted that line of code to what you posted above. However, now, Datatables throws an alert that says, "DataTables warning: table id=example - Ajax error." Do you think maybe it's because of 'formatter' => function( $d, $row)?

  • allanallan Posts: 63,686Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Possibly... What does the response from the server contain - hopefully there is an error message in there which says what the problem is. The check note that error links to shows you how to get that information.

    Allan

This discussion has been closed.