Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Fix for complex options rendering and adds table->hideColumn() for hidden columns (like primary key) #264

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/Chumper/Datatable/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Table {
*/
private $columns = array();

/**
* @var array
*/
private $hiddenColumns = array();

/**
* @var array
*/
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -248,6 +281,7 @@ public function setUrl($url)
{
$this->options['sAjaxSource'] = $url;
$this->options['bServerSide'] = true;
unset($this->optionString);
return $this;
}

Expand All @@ -259,6 +293,15 @@ public function getOptions()
return $this->options;
}

public function getOptionString()
{
if(!isset($this->optionString))
{
$this->renderOptions();
}
return $this->optionString;
}

/**
* @return array
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
));
}

Expand Down Expand Up @@ -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;
}
}
21 changes: 3 additions & 18 deletions src/views/javascript.blade.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
<script type="text/javascript">
jQuery(document).ready(function(){
// dynamic table
oTable = jQuery('#{{ $id }}').dataTable({

@foreach ($options as $k => $o)
{{ json_encode($k) }}: @if(!is_array($o)) @if(preg_match("/function/", $o)) {{ $o }} @else {{ json_encode($o) }}, @endif
@else
[{
@foreach ($o as $x => $r)
{{ json_encode($x) }}: @if(is_array($r)) {{ json_encode($r) }}, @elseif(preg_match("/function/", $r)) {{ $r }}, @else {{ json_encode($r) }} @endif
@endforeach
}],
@endif

oTable = jQuery('#{{ $id }}').DataTable({{ $options_string }});
@foreach($hidden as $column)
oTable.columns({{$column}}).visible(false);
@endforeach

@foreach ($callbacks as $k => $o)
{{ json_encode($k) }}: {{ $o }},
@endforeach

});
// custom values are available via $values array
});
</script>