Skip to content

Commit

Permalink
'added alias mapping, fixed bugs'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumper committed Feb 23, 2014
1 parent bf48633 commit b5b0e41
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 59 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ so i developed this package which in my opinion is superior.

##Known Issues

* SingleColumn search is only integrated into the Query Engine yet
* none i know of so far

##TODO

* integrate Single column search into collection engine
* fix incoming bugs
* code documentaion

Expand Down Expand Up @@ -302,6 +301,32 @@ E.g.:

Please look into the specific Columns for further information.

**setAliasMapping()**

Will advise the Datatable to return the data mapped with the column name.
So instead of
```javascript
{
"aaData":[
[3,"name","2014-02-02 23:28:14"]
],
"sEcho":9,
"iTotalRecords":2,
"iTotalDisplayRecords":1
}
```
you will get something like this as response
```javascript
{
"aaData":[
{"id":2,"name":"Datatable","created_at":"Sun, Feb 2, 2014 7:17 PM"}
],
"sEcho":2,
"iTotalRecords":2,
"iTotalDisplayRecords":1
}
```

**make()**

This will handle the input data of the request and provides the result set.
Expand Down Expand Up @@ -371,6 +396,7 @@ You can render it manually with
Will render the javascript if no view is given or the default one and will pass the class name, the options and the callbacks.

Example:

```php
$table = Datatable::table()
->addColumn('Email2','Email', "Test")
Expand Down Expand Up @@ -409,6 +435,23 @@ Will set a single callback function or an array of callbacks for the jquery call
Will add a column to the table, where the name will be rendered on the table head.
So you can provide the string that should be shown.

if you want to use the alias mapping feature of the server side table then you need to add an array like this

```php
Datatable::table()
->addColumn(array(
'id' => 'ID',
'name' => 'Name',
'created_at' => 'Erstellt'
))
->render();
```
Please note that passing an assosiative array to the addColumn function will automatically enable the alias function on the table

**setAliasMapping(boolean)**

Here you can explicitly set if the table should be aliased or not.

**countColumns()**

This will return the number of columns that will be rendered later. Mainly for testing and debugging.
Expand Down
18 changes: 12 additions & 6 deletions src/Chumper/Datatable/Columns/DateColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,31 @@ function __construct($name, $format = 2, $custom = "")
*/
public function run($model)
{

if(is_string(is_array($model) ? $model[$this->name]: $model->{$this->name}))
{
return is_array($model) ? $model[$this->name]: $model->{$this->name};
}

switch($this->format)
{
case DateColumn::DATE:
return $model[$this->name]->toDateString();
return is_array($model) ? $model[$this->name]->toDateString(): $model->{$this->name}->toDateString();
break;
case DateColumn::TIME:
return $model[$this->name]->toTimeString();
return is_array($model) ? $model[$this->name]->toTimeString(): $model->{$this->name}->toTimeString();
break;
case DateColumn::DATE_TIME:
return $model[$this->name]->toDateTimeString();
return is_array($model) ? $model[$this->name]->toDateTimeString(): $model->{$this->name}->toDateTimeString();
break;
case DateColumn::CUSTOM:
return $model[$this->name]->format($this->custom);
return is_array($model) ? $model[$this->name]->format($this->custom): $model->{$this->name}->format($this->custom);
break;
case DateColumn::FORMATTED_DATE:
return $model[$this->name]->toFormattedDateString();
return is_array($model) ? $model[$this->name]->toFormattedDateString(): $model->{$this->name}->toFormattedDateString();
break;
case DateColumn::DAY_DATE:
return $model[$this->name]->toDayDateTimeString();
return is_array($model) ? $model[$this->name]->toDayDateTimeString(): $model->{$this->name}->toDayDateTimeString();
break;

}
Expand Down
12 changes: 6 additions & 6 deletions src/Chumper/Datatable/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

use Chumper\Datatable\Engines\CollectionEngine;
use Chumper\Datatable\Engines\QueryEngine;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Input;
use Request;
use View;

/**
* Class Datatable
* @package Chumper\Datatable
*/
class Datatable {

private $columnNames = array();

/**
* @param $query
* @return QueryEngine
*/
public static function query($query)
public function query($query)
{
return new QueryEngine($query);
}
Expand All @@ -28,7 +26,7 @@ public static function query($query)
* @param $collection
* @return CollectionEngine
*/
public static function collection($collection)
public function collection($collection)
{
return new CollectionEngine($collection);
}
Expand All @@ -54,4 +52,6 @@ public static function shouldHandle()
return false;
}



}
24 changes: 23 additions & 1 deletion src/Chumper/Datatable/Engines/BaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ abstract class BaseEngine {
*/
protected $orderDirection = BaseEngine::ORDER_ASC;

/**
* @var boolean If the return should be alias mapped
*/
protected $aliasMapping = false;


function __construct()
Expand Down Expand Up @@ -199,7 +203,6 @@ public function make()
"iTotalRecords" => $this->totalCount(),
"iTotalDisplayRecords" => $this->count(),
);

return Response::json($output);
}

Expand Down Expand Up @@ -259,6 +262,12 @@ public function setRowId($function)
return $this;
}

public function setAliasMapping()
{
$this->aliasMapping = true;
return $this;
}

//-------------PRIVATE FUNCTIONS-------------------

/**
Expand Down Expand Up @@ -422,6 +431,19 @@ private function take($value)
$this->limit = $value;
}

protected function getNameByIndex($index)
{
$i = 0;
foreach($this->columns as $name => $col)
{
if($index == $i)
{
return $name;
}
$i++;
}
}

abstract protected function totalCount();
abstract protected function count();
abstract protected function internalMake(Collection $columns, array $searchColumns = array());
Expand Down
31 changes: 26 additions & 5 deletions src/Chumper/Datatable/Engines/CollectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function getArray()
public function reset()
{
$this->workingCollection = $this->collection;
return $this;
}

public function stripSearch()
Expand Down Expand Up @@ -143,20 +144,27 @@ private function doInternalSearch(Collection $columns, array $searchColumns)
$ii++;
}

$this->workingCollection = $this->workingCollection->filter(function($row) use ($value, $toSearch, $caseSensitive)
$self = $this;
$this->workingCollection = $this->workingCollection->filter(function($row) use ($value, $toSearch, $caseSensitive, $self)
{
for($i = 0; $i < count($row); $i++)
{
if(!in_array($i, $toSearch))
continue;

$column = $i;
if($self->aliasMapping)
{
$column = $self->getNameByIndex($i);
}

if($this->options['stripSearch'])
{
$search = strip_tags($row[$i]);
$search = strip_tags($row[$column]);
}
else
{
$search = $row[$i];
$search = $row[$column];
}
if($caseSensitive)
{
Expand All @@ -179,8 +187,13 @@ private function doInternalOrder()

$column = $this->orderColumn;
$stripOrder = $this->options['stripOrder'];
$this->workingCollection->sortBy(function($row) use ($column,$stripOrder) {
$self = $this;
$this->workingCollection->sortBy(function($row) use ($column,$stripOrder,$self) {

if($self->aliasMapping)
{
$column = $self->getNameByIndex($column);
}
if($stripOrder)
{
return strip_tags($row[$column]);
Expand Down Expand Up @@ -212,7 +225,15 @@ private function compileArray($columns)
$i=0;
foreach ($columns as $col)
{
$entry[$i] = $col->run($row);
if($this->aliasMapping)
{
$entry[$col->getName()] = $col->run($row);
}
else
{
$entry[$i] = $col->run($row);
}

$i++;
}
return $entry;
Expand Down
14 changes: 12 additions & 2 deletions src/Chumper/Datatable/Engines/QueryEngine.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Chumper\Datatable\Engines;

use Chumper\Datatable\Datatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -73,12 +74,14 @@ public function getArray()
public function reset()
{
$this->builder = $this->originalBuilder;
return $this;
}


public function setSearchOperator($value = "LIKE")
{
$this->options['searchOperator'] = $value;
return $this;
}

public function setSearchWithAlias()
Expand Down Expand Up @@ -194,7 +197,14 @@ private function compile($builder, $columns)
$i = 0;
foreach ($columns as $col)
{
$entry[$i] = $col->run($row);
if($this->aliasMapping)
{
$entry[$col->getName()] = $col->run($row);
}
else
{
$entry[$i] = $col->run($row);
}
$i++;
}
return $entry;
Expand All @@ -204,7 +214,7 @@ private function compile($builder, $columns)

private function doInternalOrder($builder, $columns)
{
if(!$this->orderColumn == null)
if(!is_null($this->orderColumn))
{
$i = 0;
foreach($columns as $col)
Expand Down
Loading

0 comments on commit b5b0e41

Please sign in to comment.