How To: Returning more/custom data from server side script and displaying

How To: Returning more/custom data from server side script and displaying

fbasfbas Posts: 1,094Questions: 4Answers: 0
edited July 2011 in General
Sometimes you want more than the basic data from the server side. In this example, I want to show my users how many of the items in the table have been "completed" and how many remain "incomplete". There is a boolean field in my database named bComplete for that value.

[code]
// server side php
/* Data set length of Complete items after filtering */
$sWhere ? $sWhere .= " and bComplete=1 " : $sWhere = " WHERE bComplete=1 ";
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable $sWhere
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iCompleteTotal = intval($aResultFilterTotal[0]);
[/code]

and add this value (and the inverse of it) to the JSON return object
[code]
/*
* Output
*/
$output = array(
"sEcho" => $sEcho,
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"sQuery" => $sReturnQuery,
"iCompleteTotal" => $iCompleteTotal,
"iIncompleteTotal" => ($iFilteredTotal - $iCompleteTotal),
"aaData" => array()
);
[/code]


On the client side, you can get these values from fnServerData, within the .getJSON() call. I will display the results in the sInfoPostFix
[code]
// ...
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.getJSON( sSource, aoData, function (json) {
/* Do whatever additional processing you want on the callback, then tell DataTables */
iComp = json.iCompleteTotal;
iIncomp = json.iIncompleteTotal;
spacer = '             ';

oTable.fnSettings().oLanguage.sInfoPostFix = spacer + '['+iComp+' complete, '+iIncomp+' incomplete]';

fnCallback(json)
})
},
// ...


[/code]
This discussion has been closed.