diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index 0e7898d..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 */ @@ -78,6 +83,11 @@ class Table { */ private $aliasColumns = array(); + /** + * @var String Options rendered as java literal + */ + private $optionString; + function __construct() { $this->config = Config::get('datatable::table'); @@ -121,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 @@ -139,6 +168,7 @@ public function countColumns() public function removeOption($key) { if(isset($this->options[$key])) unset($this->options[$key]); + unset($this->optionString); return $this; } @@ -162,7 +192,10 @@ public function setOptions() } } else + { throw new Exception('Invalid number of options provided for the method "setOptions"'); + } + unset($this->optionString); return $this; } @@ -248,6 +281,7 @@ public function setUrl($url) { $this->options['sAjaxSource'] = $url; $this->options['bServerSide'] = true; + unset($this->optionString); return $this; } @@ -259,6 +293,15 @@ public function getOptions() return $this->options; } + public function getOptionString() + { + if(!isset($this->optionString)) + { + $this->renderOptions(); + } + return $this->optionString; + } + /** * @return array */ @@ -313,6 +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(), + 'hidden' => $this->hiddenColumns ); if (is_array($additional_template_variables)) { @@ -352,6 +397,8 @@ public function script($view = null) 'options' => $this->options, 'callbacks' => $this->callbacks, 'id' => $this->idName, + 'options_string' => $this->getOptionString(), + 'hidden' => $this->hiddenColumns )); } @@ -432,8 +479,71 @@ private function createMapping() } $i++; } + $this->renderOptions(); $this->createdMapping = true; //dd($matching); return $matching; } + + private function renderOptions($opts = null) + { + $items = !is_null($opts) ? $opts : $this->options; + if($this->isArray($items)) + { + $self = $this; + $items = array_map(function($x) use($self) { return $self->renderOptions($x); }, $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; + } + // 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); + } + 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; + } + 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 ec23629..0cde81f 100644 --- a/src/views/javascript.blade.php +++ b/src/views/javascript.blade.php @@ -1,25 +1,10 @@