Search code examples
phpdatatablesserver-side

DataTables Server-side Processing - How to add calculated column values not from database?


I am following example at https://datatables.net/examples/data_sources/server_side

In the example code, columns data is return from database like below code

$columns = array(
    array( 'db' => 'first_name', 'dt' => 0 ),
    array( 'db' => 'last_name',  'dt' => 1 ),
    array( 'db' => 'position',   'dt' => 2 )
    );

I need to add a 4th column rating and the value is not from database but calculated in a php function. How can I do that?

array( 'db' => 'rating', 'dt' => 3 ) // need get value from php function

Below is the server side php code in the example,

$table = 'datatables_demo';
 
// Table's primary key
$primaryKey = 'id';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'first_name', 'dt' => 0 ),
    array( 'db' => 'last_name',  'dt' => 1 ),
    array( 'db' => 'position',   'dt' => 2 ),
    array( 'db' => 'office',     'dt' => 3 ),
    array(
        'db'        => 'start_date',
        'dt'        => 4,
        'formatter' => function( $d, $row ) {
            return date( 'jS M y', strtotime($d));
        }
    ),
    array(
        'db'        => 'salary',
        'dt'        => 5,
        'formatter' => function( $d, $row ) {
            return '$'.number_format($d);
        }
    )
);
 
$sql_details = array(
    'user' => '',
    'pass' => '',
    'db'   => '',
    'host' => ''
);
 
require( 'ssp.class.php' );
 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

Solution

  • Found the answer at

    https://datatables.net/forums/discussion/49799/datatable-ssp-class-php

    Need to make changes to function data_output in ssp.class.php

    static function data_output ( $columns, $data )
        {
            $out = array();
     
            for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
            $row = array();
                $params = array();
                for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
            $column = $columns[$j];
     
                    // Is there a formatter?
            if ( isset( $column['formatter'] ) ) {
                        $row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
                    }
     
            // Do we need to compute?
                    elseif (isset( $column['compute'] ) ) {
                        $params[ $column['dt'] ]['compute'] = $column['compute'];
                        $params[ $column['dt'] ]['params'][] = $data[$i][ $columns[$j]['db'] ];
                    }
     
                    else {
                        $row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
                    }
     
                }
     
                if ( !empty( $params ) ) {
     
                    foreach ( $params as $dt => $parameters) {
                        $parameterStr = "";
                        foreach ( $parameters['params'] as $p) {
                            $parameterStr .= "$p,";
                        }
                        $parameterStr = rtrim($parameterStr,",");
                        $row[ $dt ] = self::$parameters['compute']($parameterStr);
                    }
                }
                ksort($row);
                $out[] = $row;
            }
     
            return $out;
        }
     
        static function shippingMethod($parameterString) {
            $output = "";
            $parameters = explode(",",$parameterString);
            foreach($parameters as $p)
                $output .= "(".$p .")";
            return $output;
        }
     
        static function konka($parameterString) {
            $output = "";
            $parameters = explode(",",$parameterString);
            foreach($parameters as $p)
                $output .= "***".$p ."***";
            return $output;
        }