Issue Passing Variables From Generated ServerSide JSON To Clientside Ajax

Issue Passing Variables From Generated ServerSide JSON To Clientside Ajax

TabloidTabloid Posts: 2Questions: 1Answers: 0

Hi there!

Currently Moved My Datatables from being loaded client side to use serverside functionality instead (for speed), I am trying to get the sum of the entire column from the server and passing that information to the client. Here is a part of the codebase I have now.


        } ,
        "dataSrc": function (jsonString) {
                       var jsonData = JSON.parse(jsonString);
        "processing": true,
        "serverSide": true,
        "dataType": "json",
        "autoWidth": true,
        "colReorder": true,
        "keys": true,
        "deferRender": true,
        "ordering": true,

 //Adds Top Calculation (Client Side)
        "fnDrawCallback": function ( row, data, start, end, display ) {
            const table = $('#dynamictable').DataTable;
            var api = this.api(), data;
            // Remove the formatting from val
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            var disp_count = 0;
            var currentcolpos = "";
            var card_built = false;
            const sumlist = [];
            const keysumwords = ["Subtotal", "Total", "HST", "Balance Subtotal", "Balance HST", "Balance Total"];

            .every(function () {

                let column = this;
                let title = column.header().textContent;

                if (keysumwords.includes(title)){
                    if (card_built == false){
                        var element = document.getElementById("sum_card");
                        card_built = true;
                    //Result Total (Variable Im hoping to replace with the serverside result)
                    total = api
                        .column( this.index(), {search:'applied'})
                        .reduce( function (a, b) {
                            return intVal(a) + intVal(b);
                        }, 0 );

                        underscored_title = title.split(' ').join('_');
                        divid = 'sum_'+ underscored_title;

                        if (disp_count % 3 == 0){
                            var newDiv = document.createElement('div');
                            colid = 'sum_'+ disp_count;
                            currentcolpos = colid;

                            var newDiv2 = document.createElement('div');
                            $(`#${divid}`).html(`<b>${title}:</b> <span class="dollars">`+ numberWithCommas(total.toFixed(2)) + '</span>');
                            var newDiv2 = document.createElement('div');
                            $(`#${divid}`).html(`<b>${title}:</b> <span class="dollars">`+ numberWithCommas(total.toFixed(2)) + '</span>');

                        disp_count += 1;

I have edited the ssp.class.php file to provide me with the data and the json output looks something like this:


(data portion has been removed but is present in the json and tables display properly, JSON is validated)

//ssp.class.php modified section

    $keysumwords = ["Subtotal", "Total", "HST", "Balance_Subtotal", "Balance_HST", "Balance_Total"];
        include 'base/config/dbconnect.php'; // DB Connect file inclusion

        $colNameQuery = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$dbname."' AND table_name = ?";
                        $colName = $conn->prepare($colNameQuery);
                        $colNameCount = $colName->rowCount();
                        $colNames = $colName->fetchAll(PDO::FETCH_ASSOC);
        $SumCol = [];

        foreach ($colNames as $name) {
            $colname = $name['COLUMN_NAME'];
            if (in_array($colname, $keysumwords)){

                $divid = 'sum_' . $colname;
                $Sum = self::sql_exec( $db, $bindings,
                    "SELECT SUM(`$colname`)
                    FROM   `$table`
                $array_col = array( $divid => $Sum[0][0]);
                array_push($SumCol ,$array_col);



         * Output
        return array(
            "draw"            => isset ( $request['draw'] ) ?
                intval( $request['draw'] ) :
            "recordsTotal"    => intval( $recordsTotal ),
            "recordsFiltered" => intval( $recordsFiltered ),
            "Sum"             => $SumCol,
            "data"            => self::data_output( $columns, $data )

The goal is to access the array passed from the server and displayed to the client eg. data['Sum']['sum_Subtotal'] to get the value of 200, however i'm having trouble even accessing the json properly through some examples on the forum.

The Addtional Code is The Old Client Side Summation Of Relavent Columns And Display, I intend to replace the calculated total with the total already calculated on the serverside and present with the JSON, though its not neccesary relavent as once I can get the variable from json I should be fine, I have included it anyways. The function currently works incorrectly since it only sums whats visible on the page hence needing to be calculated serverside and passed in.

PS. Sorry If i'm not clear enouph with this, this is my first time using datatables and posting on this forum. Also I won't be able to provide as a test case as there is way more code than whats currently posted.

This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 19,444Questions: 26Answers: 4,607
    Answer ✓

    Here are some options to access the JSON data:

    1. Use the xhr event which has a json parameter.
    2. Or use the ajax.dataSrc which gives access to the JSON data.
    3. Use the ajax.json() API to access the JSON data outside of 1 and 2.

    Looks like you might want to use this in drawCallback. You can do something like this:

      drawCallback: function () {
        var api = this.api();

    Running example:


  • TabloidTabloid Posts: 2Questions: 1Answers: 0

    Thank you so much! Can't believe it was that simple! Been scratching my head at this for too long.

Sign In or Register to comment.