From ed93869ac395579ff8ee2cd247039e181a5ae52b Mon Sep 17 00:00:00 2001 From: Tigran Gabrielyan Date: Fri, 6 Feb 2015 14:55:50 -0800 Subject: [PATCH] Added `autoRender` and `autoData` options --- .../Component/DataTable/DataTableConfig.php | 2 ++ Controller/Component/DataTableComponent.php | 32 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Controller/Component/DataTable/DataTableConfig.php b/Controller/Component/DataTable/DataTableConfig.php index 1f9578e..d85f2cf 100644 --- a/Controller/Component/DataTable/DataTableConfig.php +++ b/Controller/Component/DataTable/DataTableConfig.php @@ -16,6 +16,8 @@ class DataTableConfig { * @var array */ public $defaults = array( + 'autoData' => true, + 'autoRender' => true, 'columns' => array(), 'conditions' => array(), 'maxLimit' => 100, diff --git a/Controller/Component/DataTableComponent.php b/Controller/Component/DataTableComponent.php index 99e4a22..f94efaf 100644 --- a/Controller/Component/DataTableComponent.php +++ b/Controller/Component/DataTableComponent.php @@ -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; }