Server-side / multiply two columns and get value from function.

Server-side / multiply two columns and get value from function.

neurofunkneurofunk Posts: 14Questions: 5Answers: 1

Hello there,

(Based on this example: https://www.datatables.net/examples/server_side/row_details.html )
I've got two columns from database:

$columns = array(
    array( 'db' => 'rate',     'dt' => 'episodes' ),   // integer
    array( 'db' => 'state' ,   'dt' => 'length'),      // integer
);

I want to multiply (episodes * length), then send it to the function:

function convert_minutes_to_string($minutes){
    ...
    return $time_string; // eg. 2d 3h 12m
}

And get $time_string to my datatable

var dt = $('#example').DataTable({
    ....
    "columns": [            
        { "data" : "time_string" },
    ],
});

But I have no idea how to do that, anyone can help me?

Answers

  • almircamposalmircampos Posts: 13Questions: 5Answers: 0
    edited July 2015

    Hi, @neurofunk.

    I'm not a PHP programmer, but what I'm would do in Java is do the calculations on the server side and send the data to the front-end something like:

    $columns = array(
        array( 'db' => 'rate',     'dt' => 'episodes' ),   // integer
        array( 'db' => 'state' ,   'dt' => 'length'),      // integer
        array( 'db' => 'total_length' ,   'dt' => 'episodes * length'),      // integer
    );
    

    But I think you can also use the render options on the front-end to make the calculations, but I didn't test it yet.

    Hope it helps.

    Almir.

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

    Unfortunately it doesn't work, but you inspired me and I found the solution:

    {    
        "data" : "episodes"
        "visible" : false
    },
    
    {   
        "data": "length" ,
        "visible": true,
        "render" : function(data , type , row){
            var total = data * row['episodes']
        return xtime(total)
    },
    

    But this solution makes next question:

    How to make this column sortable by var total (by int)?

This discussion has been closed.