Server side / formatter

Server side / formatter

neurofunkneurofunk Posts: 14Questions: 5Answers: 1

I have one problem. I've got some columns (for example):

array(
    'db' => 'length' ,
    'dt'  => 'length' 
    ),
array(
    'db' => 'height' , 
    'dt' => 'height'
    )

I wanna do some mathematical operations necessarily(!) on server side (Yup, I know. I can do the same things on the front side using "render", but I want to do this on server), for simplest example:

height * length 

I tried to do something with formatter:

array(
        'db' => 'height' ,
        'dt'  => 'height' 
    ),
array(
        'db'        => 'length',
        'dt'        => 'length',
        'formatter' => function( $d, $row ) {
            return $d * ???height???;  // how can i get the height?
        }
    ),

but I'm beginner and i need to help with this ;)

Answers

  • neurofunkneurofunk Posts: 14Questions: 5Answers: 1
    edited July 2015

    I found partially solution

    array(
                'db'         => 'length',
                'dt'         => 'eval',
                'formatter' => function( $d, $row ) {
                    return $d * $row[3];
                }
            ),
    

    ...but datatables still sorts this column by 'length' not by value returned by formatter. How to make that column sortable by that value?

        "columns": [
                
                {   //0
                    "data" : "eval"
                },
        ]
    
  • AshbjornAshbjorn Posts: 55Questions: 2Answers: 16

    Be aware that when sorting the datatable on an arbitrary value this is interpreted as ordering by descending values on that column. Do you perhaps mean filtering on this computed value?

    Also you indicate that you must calculate server-side, but yet you provide us with a partial solution done client-side?

    If you are going to do this server-side I would suggest adding a computed column to your array that already contains the calculated value and then you can address that column in DataTables like any other.

    Hope this helps,

This discussion has been closed.