From bed9e5a8db1fc287cc3f99391ca79b382ce7bcf1 Mon Sep 17 00:00:00 2001 From: Todd Blanchard Date: Thu, 19 Mar 2015 16:04:37 -0700 Subject: [PATCH 1/5] added recursive option rendering --- src/Chumper/Datatable/Table.php | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index 0e7898d..e98459e 100644 --- a/src/Chumper/Datatable/Table.php +++ b/src/Chumper/Datatable/Table.php @@ -78,6 +78,11 @@ class Table { */ private $aliasColumns = array(); + /** + * @var String Options rendered as java literal + */ + private $optionString; + function __construct() { $this->config = Config::get('datatable::table'); @@ -139,6 +144,7 @@ public function countColumns() public function removeOption($key) { if(isset($this->options[$key])) unset($this->options[$key]); + unset($this->optionString); return $this; } @@ -162,7 +168,10 @@ public function setOptions() } } else + { throw new Exception('Invalid number of options provided for the method "setOptions"'); + } + unset($this->optionString); return $this; } @@ -248,6 +257,7 @@ public function setUrl($url) { $this->options['sAjaxSource'] = $url; $this->options['bServerSide'] = true; + unset($this->optionString); return $this; } @@ -259,6 +269,15 @@ public function getOptions() return $this->options; } + public function getOptionString() + { + if(!isset($this->optionString)) + { + $this->renderOptions(); + } + return $this->optionString; + } + /** * @return array */ @@ -313,6 +332,7 @@ public function render($view = null, array $additional_template_variables = null 'noScript' => $this->noScript, 'id' => $this->idName, 'class' => $this->className, + 'options_string' => $this->getOptionString() ); if (is_array($additional_template_variables)) { @@ -432,8 +452,63 @@ private function createMapping() } $i++; } + $this->renderOptions(); $this->createdMapping = true; //dd($matching); return $matching; } + + private function renderOptions($opts = null) + { + $items = $opts ? $opts : $this->options; + if($this->isArray($items)) + { + $items = array_map($this->renderOptions, $items); + $result = '['.implode(",\n", $items).']'; + } + else if(is_array($items)) + { + $elements = []; + foreach($items as $key => $value) + { + $elements[] = ('"'. $key .'": ' . $this->renderOptions($value)); + } + + // if root level - add in callbacks + if(is_null($opts) && is_array($this->callbacks)) + { + foreach($this->callbacks as $key => $value) + { + $elements[] = ('"'.$key.'": ' . $value); + } + } + + $result = '{'.implode(",\n", $elements).'}'; + } + else if(strpos(trim($items), "function") === 0) + { + $result = $items; + } + else + { + $result = json_encode($items); + } + if($opts === null) + { + $this->optionString = $result; + } + return $result; + } + + private function isArray($item) + { + if(is_array($item)) + { + $idx = 0; + foreach ($item as $key => $value) + { + if($key != $idx++) return false; + } + } + } } \ No newline at end of file From a194b6d83b14ab469f3d18f68f203c3b3065ec03 Mon Sep 17 00:00:00 2001 From: Todd Blanchard Date: Thu, 19 Mar 2015 16:05:34 -0700 Subject: [PATCH 2/5] added recursive option rendering to template --- src/views/javascript.blade.php | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/views/javascript.blade.php b/src/views/javascript.blade.php index ec23629..e39a378 100644 --- a/src/views/javascript.blade.php +++ b/src/views/javascript.blade.php @@ -1,25 +1,7 @@ From f3d76333c01d73b8dd169afed7cdbd84400eb57a Mon Sep 17 00:00:00 2001 From: Todd Blanchard Date: Thu, 19 Mar 2015 16:16:42 -0700 Subject: [PATCH 3/5] added null/false clarifier --- src/Chumper/Datatable/Table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index e98459e..ab87192 100644 --- a/src/Chumper/Datatable/Table.php +++ b/src/Chumper/Datatable/Table.php @@ -460,7 +460,7 @@ private function createMapping() private function renderOptions($opts = null) { - $items = $opts ? $opts : $this->options; + $items = !is_null($opts) ? $opts : $this->options; if($this->isArray($items)) { $items = array_map($this->renderOptions, $items); From 15c4f245db284e7f29e8f4eb479da9d7ccb521ff Mon Sep 17 00:00:00 2001 From: Todd Blanchard Date: Thu, 19 Mar 2015 16:22:38 -0700 Subject: [PATCH 4/5] fixed typo --- src/views/javascript.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/javascript.blade.php b/src/views/javascript.blade.php index e39a378..e652026 100644 --- a/src/views/javascript.blade.php +++ b/src/views/javascript.blade.php @@ -1,7 +1,7 @@ From e0fc43b7277096c3e26224e4ae38d52ec78757e6 Mon Sep 17 00:00:00 2001 From: Todd Blanchard Date: Fri, 20 Mar 2015 01:07:30 -0700 Subject: [PATCH 5/5] fixed recursive rendering of options, added javascript: scheme support for raw js, added table->hideColumn --- src/Chumper/Datatable/Table.php | 39 +++++++++++++++++++++++++++++++-- src/views/javascript.blade.php | 5 ++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index ab87192..5c1e142 100644 --- a/src/Chumper/Datatable/Table.php +++ b/src/Chumper/Datatable/Table.php @@ -21,6 +21,11 @@ class Table { */ private $columns = array(); + /** + * @var array + */ + private $hiddenColumns = array(); + /** * @var array */ @@ -126,6 +131,25 @@ public function addColumn() return $this; } + /** + * @return $this + */ + public function hideColumn() + { + foreach(func_get_args() as $title) + { + if(in_array($title, $this->columns)) + { + $this->hiddenColumns[] = array_search($title, $this->columns); + } + else if(in_array($title, $this->aliasColumns)) + { + $this->hiddenColumns[] = array_search($title,$this->aliasColumns); + } + } + return $this; + } + /** * Count the number of columns in the datatable. * @return int @@ -332,7 +356,8 @@ public function render($view = null, array $additional_template_variables = null 'noScript' => $this->noScript, 'id' => $this->idName, 'class' => $this->className, - 'options_string' => $this->getOptionString() + 'options_string' => $this->getOptionString(), + 'hidden' => $this->hiddenColumns ); if (is_array($additional_template_variables)) { @@ -372,6 +397,8 @@ public function script($view = null) 'options' => $this->options, 'callbacks' => $this->callbacks, 'id' => $this->idName, + 'options_string' => $this->getOptionString(), + 'hidden' => $this->hiddenColumns )); } @@ -463,7 +490,8 @@ private function renderOptions($opts = null) $items = !is_null($opts) ? $opts : $this->options; if($this->isArray($items)) { - $items = array_map($this->renderOptions, $items); + $self = $this; + $items = array_map(function($x) use($self) { return $self->renderOptions($x); }, $items); $result = '['.implode(",\n", $items).']'; } else if(is_array($items)) @@ -489,6 +517,11 @@ private function renderOptions($opts = null) { $result = $items; } + // javascript literal code - print it - minus the uri scheme - unquoted + else if(strpos(trim($items), "javascript:") === 0) + { + $result = substr(trim($items),strlen("javascript:")); + } else { $result = json_encode($items); @@ -509,6 +542,8 @@ private function isArray($item) { if($key != $idx++) return false; } + return true; } + return false; } } \ No newline at end of file diff --git a/src/views/javascript.blade.php b/src/views/javascript.blade.php index e652026..0cde81f 100644 --- a/src/views/javascript.blade.php +++ b/src/views/javascript.blade.php @@ -1,7 +1,10 @@