Displaying custom data from the same GET request as Datatables

Displaying custom data from the same GET request as Datatables

gordyrgordyr Posts: 35Questions: 0Answers: 0
edited December 2011 in General
I have modified my server side code to output A users avatar as well as the table data like so:-

[code]
$output = array(

"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array(),
"avatar" => bp_core_fetch_avatar( array('item_id' => $uid, 'type' => 'thumb') )
);
[/code]

This now produces all the data I want put into datatbles and returns in the JSON also.

Works great, but I would like to be able to map this data into an element.

Currently I am using a second PHP file, custom written by myself to return this and it works perfectly but I see no point in having two server hits when I can easily pass this data from within the same GET request as the table information.

How can I retrieve this HTML?

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited December 2011
    you can access the json from this GET/POST in fnServerData by intercepting it in the jQuery.ajax() "success" routine :

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "sAjaxSource": "data_source.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
    "dataType": 'json',
    "url": sSource,
    "data": aoData,
    "success": function(data, textStatus, jqXHR) {
    // find avatar attached to your "data"
    console.log(data.avatar);

    fnCallback(data, textStatus, jqXHR);
    }
    } );
    }
    } );
    } );
    [/code]

    see http://www.datatables.net/ref#fnServerData
    and http://api.jquery.com/jQuery.ajax/
  • gordyrgordyr Posts: 35Questions: 0Answers: 0
    Fabulous! Answers my question perfectly... Many thanks.
This discussion has been closed.