0

I have a jQuery Datatable with 2 rows in the header. I'd like to populate the 2nd row of this header with some total values that I am passing back from the server, in JSON, along with all the data for the content of the table.

I've looked and looked to find a way to do this, but I'm struggling.

Can anyone tell me what the best way to get these values in to the header are please once the data has been obtained from the server?

Here is how I am getting the JSON back from the server and populating the rows of the table:

$.ajax({
    url: "<?php echo $this->url($reportResource)?>"
}).done(function(data) {
    // populate the data in to the rows of the table
    reportTable.rows.add(data.rows).draw();

    // the data param here contains an item in the array which contains all 
    // of the totals I'd like to put in the 2nd header row:
    var totalValues = data.totals;
});

Here is the HTML for the table:

<table class="table table-striped" id="report-table">
    <br/>
    <thead>
        <th>Impressions</th>
        <th>Sold</th>
        <th>Unsold</th>
        <th>Percentage</th>
        <th>eCPM</th>
        <th>Revenue</th>
        <th>Commision</th>
        <th>Net</th>
    </tr>
    <tr>
        <th style="text-align:right">Totals:</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

And the JSON returned is:

{
 "rows": {
     "0":{"site":"<website1>","imps":20276,"sold":17308,"unsold":2968,"percentage":85.3620043401,"eCpm":0.0909454034326,"grossRevenue":1.844009,"commission":0.5532027,"netRevenue":1.2908063},
     "1":{"site":"<website2>","imps":4485,"sold":3900,"unsold":585,"percentage":86.9565217391,"eCpm":0.0833068004459,"grossRevenue":0.373631,"commission":0.1120893,"netRevenue":0.2615417},
     "2":{"site":"<website3>","imps":37,"sold":34,"unsold":3,"percentage":91.8918918919,"eCpm":0.0665405405405,"grossRevenue":0.002462,"commission":0.0007386,"netRevenue":0.0017234},
 }
 "totals":{"imps":24798,"sold":21242,"unsold":3556,"percentage":85.6601338818,"eCpm":0.0895274618921,"grossRevenue":2.220102,"commission":0.6660306,"netRevenue":1.5540714}
}

Your help is much appreciated.

4
  • How are you retrieving the JSON data? getJSON()? api.jquery.com/jquery.getjson Commented Aug 15, 2014 at 12:27
  • Thanks for your reply, I have added a code snippet to show how i'm getting the data and populating the rows of the table Commented Aug 15, 2014 at 12:41
  • Can you add a bit more? like the data format that is returned, and the data table html? thanks Commented Aug 15, 2014 at 13:07
  • No problem, I have added the table HTML and also an example of the JSON data returned. Many thanks. Commented Aug 15, 2014 at 13:47

1 Answer 1

1

To begin with your JSON response is not valid. You might want to have a look at that - there is a comma in the end of 2:{} - remove that comma, and you are also missing a comma before "totals".

In your success function, you can append <td> with the values, as I tried to illustrate here:

var row = $('#totals');

// add tds to row
for (value in data.totals) {
    row.append('<td>'+ data.totals[value] +'</td>');
}

Fiddle: http://jsfiddle.net/z7p3qnnt/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I have indeed followed the principles in your answer and updated them using jQuery etc. I guess originally I was wondering if the datatables API allowed for these to be updated. But this solution is perfectly good enough!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.