Skip to content
This repository was archived by the owner on Apr 17, 2019. It is now read-only.
43 changes: 32 additions & 11 deletions src/Chumper/Datatable/Engines/BaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ abstract class BaseEngine {
* @var bool If you need to display all records.
*/
protected $enableDisplayAll = false;

/**
* @var mixed Additional data which passed from server to client.
*/
protected $additionalData = null;

function __construct()
{
$this->columns = new Collection();
Expand Down Expand Up @@ -251,6 +257,7 @@ public function make()
"sEcho" => intval($this->sEcho),
"iTotalRecords" => $this->totalCount(),
"iTotalDisplayRecords" => $this->count(),
"aaAdditional" => $this->additionalData,
);
return Response::json($output);
}
Expand Down Expand Up @@ -343,6 +350,12 @@ public function setExactMatchColumns($columnNames)
return $this;
}

public function setAdditionalData($data)
{
$this->additionalData = $data;
return $this;
}

public function getRowClass()
{
return $this->rowClass;
Expand Down Expand Up @@ -423,14 +436,16 @@ protected function handlesSearch($value)
protected function handleiSortCol_0($value)
{
if(Input::get('sSortDir_0') == 'desc')
$direction = BaseEngine::ORDER_DESC;
$direction[$value] = BaseEngine::ORDER_DESC;
else
$direction = BaseEngine::ORDER_ASC;
$direction[$value] = BaseEngine::ORDER_ASC;

$columns = array();
//check if order is allowed
if(empty($this->orderColumns))
{
$this->order(array(0 => $value, 1 => $this->getNameByIndex($value)), $direction);
$columns[] = array(0 => $value, 1 => $this->getNameByIndex($value));
$this->order($columns, $direction);
return;
}

Expand All @@ -448,16 +463,22 @@ protected function handleiSortCol_0($value)
}
}

$i = 0;
foreach($this->columns as $name => $column)
{
if($i == $value && in_array($name, $cleanNames))
{
$this->order(array(0 => $value, 1 => $this->orderColumns[array_search($name,$cleanNames)]), $direction);
return;
$iSortingCols = Input::get('iSortingCols');
$sortingCols[] = $value;
for($i = 1; $i < $iSortingCols; $i++) {
$isc = Input::get('iSortCol_'.$i);
$sortingCols[] = $isc;
$direction[$isc] = Input::get('sSortDir_'.$i);
}

$allColumns = array_keys($this->columns->all());
foreach ($sortingCols as $num) {
if(in_array($allColumns[$num], $cleanNames)) {
$columns[] = array(0 => $num, 1 => $this->orderColumns[array_search($allColumns[$num],$cleanNames)]);
}
$i++;
}
$this->order($columns, $direction);
return;
}

/**
Expand Down
41 changes: 24 additions & 17 deletions src/Chumper/Datatable/Engines/QueryEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class QueryEngine extends BaseEngine {
'counter' => 0,
'noGroupByOnCount' => false,
'distinctCountGroup'=> false,
'emptyAtEnd' => false,
'returnQuery' => false,
);

Expand Down Expand Up @@ -70,7 +71,12 @@ public function totalCount()
{
$this->originalBuilder->groups = null;
}
return $this->originalBuilder->count();
if($this->options['searchWithAlias']) {
$cnt = count($this->originalBuilder->get());
} else {
$cnt = $this->originalBuilder->count();
}
return $cnt;
}

public function getArray()
Expand All @@ -97,6 +103,12 @@ public function setSearchWithAlias($value = true)
return $this;
}

public function setEmptyAtEnd()
{
$this->options['emptyAtEnd'] = true;
return $this;
}

public function setNoGroupByOnCount($value = true)
{
$this->options['noGroupByOnCount'] = (bool)$value;
Expand Down Expand Up @@ -316,23 +328,18 @@ private function doInternalOrder($builder, $columns)
//var_dump($this->orderColumn);
if(!is_null($this->orderColumn))
{
$i = 0;
foreach($columns as $col)
{

if($i === (int) $this->orderColumn[0])
{
if(strrpos($this->orderColumn[1], ':')){
$c = explode(':', $this->orderColumn[1]);
if(isset($c[2]))
$c[1] .= "($c[2])";
$builder = $builder->orderByRaw("cast($c[0] as $c[1]) ".$this->orderDirection);
}
else
$builder = $builder->orderBy($col->getName(), $this->orderDirection);
return $builder;
foreach ($this->orderColumn as $ordCol) {
if(strrpos($ordCol[1], ':')){
$c = explode(':', $ordCol[1]);
if(isset($c[2]))
$c[1] .= "($c[2])";
$prefix = $this->options['emptyAtEnd'] ? "ISNULL({$c[0]}) asc," : '';
$builder = $builder->orderByRaw($prefix." cast($c[0] as $c[1]) ".$this->orderDirection[$ordCol[0]]);
}
else {
$prefix = $this->options['emptyAtEnd'] ? "ISNULL({$ordCol[1]}) asc," : '';
$builder = $builder->orderByRaw($prefix.' '.$ordCol[1].' '.$this->orderDirection[$ordCol[0]]);
}
$i++;
}
}
return $builder;
Expand Down
18 changes: 18 additions & 0 deletions src/Chumper/Datatable/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class Table {
*/
protected $className;

/**
* @var String The footer's display mode
*/
protected $footerMode = 'hidden';

/**
* @var String The view used to render the table
*/
Expand Down Expand Up @@ -313,6 +318,7 @@ public function render($view = null, array $additional_template_variables = null
'noScript' => $this->noScript,
'id' => $this->idName,
'class' => $this->className,
'footerMode'=> $this->footerMode,
);

if (is_array($additional_template_variables)) {
Expand Down Expand Up @@ -393,6 +399,18 @@ public function setClass($class)
return $this;
}

/**
* Set the footer display mode.
*
* @param $value the one of next values: 'hidden', 'columns', 'empty'
* @return $this
*/
public function showFooter($value = 'columns')
{
$this->footerMode = $value;
return $this;
}

/**
* Advise the Datatable to return the data mapped with the column name.
*
Expand Down
15 changes: 11 additions & 4 deletions src/views/javascript.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
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
{{ json_encode($k) }}: @if(!is_array($o)) @if(preg_match("/function/", $o)) {{ $o }}, @else {{ json_encode($o) }}, @endif
@elseif(key($o) === 0) {{-- if we have an array, no need to print keys --}}
[
@foreach ($o as $r)
@if(is_array($r)) {{ json_encode($r) }}, @elseif(preg_match("/function/", $r)) {{ $r }}, @else {{ json_encode($r) }}, @endif

@endforeach
],
@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
{{ json_encode($x) }}: @if(is_array($r)) {{ json_encode($r) }}, @elseif(preg_match("/function/", $r)) {{ $r }}, @else {{ json_encode($r) }}, @endif
@endforeach
}],
},
@endif

@endforeach
Expand Down
9 changes: 9 additions & 0 deletions src/views/template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
@endforeach
</tr>
</thead>
@if ($footerMode !== 'hidden')
<tfoot>
<tr>
@foreach($columns as $i => $c)
<th align="center" valign="middle" class="footer{{ $i }}">@if($footerMode === 'columns') {{ $c }} @endif</th>
@endforeach
</tr>
</tfoot>
@endif
<tbody>
@foreach($data as $d)
<tr>
Expand Down