Skip to content

Commit

Permalink
Added autoRender and autoData options
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrang committed Feb 7, 2015
1 parent 03b936c commit ed93869
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Controller/Component/DataTable/DataTableConfig.php
Expand Up @@ -16,6 +16,8 @@ class DataTableConfig {
* @var array
*/
public $defaults = array(
'autoData' => true,
'autoRender' => true,
'columns' => array(),
'conditions' => array(),
'maxLimit' => 100,
Expand Down
32 changes: 27 additions & 5 deletions Controller/Component/DataTableComponent.php
Expand Up @@ -60,17 +60,39 @@ public function paginate($name = null, $scope = array()) {
$iTotalDisplayRecords = $Model->find('count', $config->getCountQuery());
$results = $Model->find('all', $config->getQuery());

$aaData = array();
if ($config->autoData) {
foreach ($results as $result) {
$row = [];
foreach ($config->columns as $column => $options) {
if (!$options['useField']) {
$row[] = null;
break;
}
$value = Hash::extract($result, $column);
$row[] = $value ? $value[0] : null;
}
$aaData[] = $row;
}
}

$dataTableData = array(
'iTotalRecords' => $iTotalRecords,
'iTotalDisplayRecords' => $iTotalDisplayRecords,
'sEcho' => (int) Hash::get($this->_getParams(), 'sEcho'),
'aaData' => array(),
'aaData' => $aaData,
);

$this->Controller->viewClass = 'DataTable.DataTableResponse';
$this->Controller->view = $config->view;
$this->Controller->set($config->viewVar, $results);
$this->Controller->set(compact('dataTableData'));
if ($config->autoData && $config->autoRender) {
$this->Controller->viewClass = 'Json';
$this->Controller->set(compact('dataTableData'));
$this->Controller->set('_serialize', 'dataTableData');
} else {
$this->Controller->viewClass = 'DataTable.DataTableResponse';
$this->Controller->view = $config->view;
$this->Controller->set($config->viewVar, $results);
$this->Controller->set(compact('dataTableData'));
}

return $config;
}
Expand Down

0 comments on commit ed93869

Please sign in to comment.