IgnitedDatatables/Ignited-Datatables — Issue #10: Callbacks & Objects
Author: jellehak
Created: 2011-11-23 21:25:58+00:00
State: open

Gear code !! But since I use the datagrid with some more php function styling & use object rather than arrays... I changed the generate part some to; Maybe someone finds it usefull or it could be ported in a new release?

var $callback;
protected function produce_output($charset)
{
$aaData = array();
$rResult = $this->get_display_result();
$iTotal = $this->get_total_results();
$iFilteredTotal = $this->get_total_results(TRUE);

$result = $rResult->result();
//Tune the data?
$function = $this->callback;
$function( $result);

foreach($result as $row_key => $row_val)
{
$c = 0;
foreach($row_val as $k => $v) {
$row_val_arr[$c] = $v;
$c++;
}
$aaData[$row_key] = $row_val_arr;

}

$sColumns = array_diff($this->columns, $this->unset_columns);
$sColumns = array_merge_recursive($sColumns, array_keys($this->add_columns));

$sOutput = array
(
'sEcho' => intval($this->ci->input->post('sEcho')),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => $aaData,
'sColumns' => implode(',', $sColumns)
);

if(strtolower($charset) == 'utf-8')
return json_encode($sOutput);
else
return $this->jsonify($sOutput);
}

Now you can use the callback to php style your data as follows:
//Inside the class
....
$this->datatables->callback = "prettify";
$this->datatables->generate();

//Outside the Class
function prettify($result) {
foreach($result as $row) {
$row->website = auto_link($row->website, 'url', TRUE);
}

}