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

Commit b5b0e41

Browse files
author
Chumper
committed
'added alias mapping, fixed bugs'
1 parent bf48633 commit b5b0e41

File tree

10 files changed

+237
-59
lines changed

10 files changed

+237
-59
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ so i developed this package which in my opinion is superior.
1010

1111
##Known Issues
1212

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

1515
##TODO
1616

17-
* integrate Single column search into collection engine
1817
* fix incoming bugs
1918
* code documentaion
2019

@@ -302,6 +301,32 @@ E.g.:
302301

303302
Please look into the specific Columns for further information.
304303

304+
**setAliasMapping()**
305+
306+
Will advise the Datatable to return the data mapped with the column name.
307+
So instead of
308+
```javascript
309+
{
310+
"aaData":[
311+
[3,"name","2014-02-02 23:28:14"]
312+
],
313+
"sEcho":9,
314+
"iTotalRecords":2,
315+
"iTotalDisplayRecords":1
316+
}
317+
```
318+
you will get something like this as response
319+
```javascript
320+
{
321+
"aaData":[
322+
{"id":2,"name":"Datatable","created_at":"Sun, Feb 2, 2014 7:17 PM"}
323+
],
324+
"sEcho":2,
325+
"iTotalRecords":2,
326+
"iTotalDisplayRecords":1
327+
}
328+
```
329+
305330
**make()**
306331

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

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

438+
if you want to use the alias mapping feature of the server side table then you need to add an array like this
439+
440+
```php
441+
Datatable::table()
442+
->addColumn(array(
443+
'id' => 'ID',
444+
'name' => 'Name',
445+
'created_at' => 'Erstellt'
446+
))
447+
->render();
448+
```
449+
Please note that passing an assosiative array to the addColumn function will automatically enable the alias function on the table
450+
451+
**setAliasMapping(boolean)**
452+
453+
Here you can explicitly set if the table should be aliased or not.
454+
412455
**countColumns()**
413456

414457
This will return the number of columns that will be rendered later. Mainly for testing and debugging.

src/Chumper/Datatable/Columns/DateColumn.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,31 @@ function __construct($name, $format = 2, $custom = "")
3737
*/
3838
public function run($model)
3939
{
40+
41+
if(is_string(is_array($model) ? $model[$this->name]: $model->{$this->name}))
42+
{
43+
return is_array($model) ? $model[$this->name]: $model->{$this->name};
44+
}
45+
4046
switch($this->format)
4147
{
4248
case DateColumn::DATE:
43-
return $model[$this->name]->toDateString();
49+
return is_array($model) ? $model[$this->name]->toDateString(): $model->{$this->name}->toDateString();
4450
break;
4551
case DateColumn::TIME:
46-
return $model[$this->name]->toTimeString();
52+
return is_array($model) ? $model[$this->name]->toTimeString(): $model->{$this->name}->toTimeString();
4753
break;
4854
case DateColumn::DATE_TIME:
49-
return $model[$this->name]->toDateTimeString();
55+
return is_array($model) ? $model[$this->name]->toDateTimeString(): $model->{$this->name}->toDateTimeString();
5056
break;
5157
case DateColumn::CUSTOM:
52-
return $model[$this->name]->format($this->custom);
58+
return is_array($model) ? $model[$this->name]->format($this->custom): $model->{$this->name}->format($this->custom);
5359
break;
5460
case DateColumn::FORMATTED_DATE:
55-
return $model[$this->name]->toFormattedDateString();
61+
return is_array($model) ? $model[$this->name]->toFormattedDateString(): $model->{$this->name}->toFormattedDateString();
5662
break;
5763
case DateColumn::DAY_DATE:
58-
return $model[$this->name]->toDayDateTimeString();
64+
return is_array($model) ? $model[$this->name]->toDayDateTimeString(): $model->{$this->name}->toDayDateTimeString();
5965
break;
6066

6167
}

src/Chumper/Datatable/Datatable.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
use Chumper\Datatable\Engines\CollectionEngine;
44
use Chumper\Datatable\Engines\QueryEngine;
5-
use Exception;
6-
use Illuminate\Database\Eloquent\Builder;
7-
use Illuminate\Support\Collection;
85
use Input;
96
use Request;
10-
use View;
117

128
/**
139
* Class Datatable
1410
* @package Chumper\Datatable
1511
*/
1612
class Datatable {
1713

14+
private $columnNames = array();
15+
1816
/**
1917
* @param $query
2018
* @return QueryEngine
2119
*/
22-
public static function query($query)
20+
public function query($query)
2321
{
2422
return new QueryEngine($query);
2523
}
@@ -28,7 +26,7 @@ public static function query($query)
2826
* @param $collection
2927
* @return CollectionEngine
3028
*/
31-
public static function collection($collection)
29+
public function collection($collection)
3230
{
3331
return new CollectionEngine($collection);
3432
}
@@ -54,4 +52,6 @@ public static function shouldHandle()
5452
return false;
5553
}
5654

55+
56+
5757
}

src/Chumper/Datatable/Engines/BaseEngine.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ abstract class BaseEngine {
8383
*/
8484
protected $orderDirection = BaseEngine::ORDER_ASC;
8585

86+
/**
87+
* @var boolean If the return should be alias mapped
88+
*/
89+
protected $aliasMapping = false;
8690

8791

8892
function __construct()
@@ -199,7 +203,6 @@ public function make()
199203
"iTotalRecords" => $this->totalCount(),
200204
"iTotalDisplayRecords" => $this->count(),
201205
);
202-
203206
return Response::json($output);
204207
}
205208

@@ -259,6 +262,12 @@ public function setRowId($function)
259262
return $this;
260263
}
261264

265+
public function setAliasMapping()
266+
{
267+
$this->aliasMapping = true;
268+
return $this;
269+
}
270+
262271
//-------------PRIVATE FUNCTIONS-------------------
263272

264273
/**
@@ -422,6 +431,19 @@ private function take($value)
422431
$this->limit = $value;
423432
}
424433

434+
protected function getNameByIndex($index)
435+
{
436+
$i = 0;
437+
foreach($this->columns as $name => $col)
438+
{
439+
if($index == $i)
440+
{
441+
return $name;
442+
}
443+
$i++;
444+
}
445+
}
446+
425447
abstract protected function totalCount();
426448
abstract protected function count();
427449
abstract protected function internalMake(Collection $columns, array $searchColumns = array());

src/Chumper/Datatable/Engines/CollectionEngine.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function getArray()
7979
public function reset()
8080
{
8181
$this->workingCollection = $this->collection;
82+
return $this;
8283
}
8384

8485
public function stripSearch()
@@ -143,20 +144,27 @@ private function doInternalSearch(Collection $columns, array $searchColumns)
143144
$ii++;
144145
}
145146

146-
$this->workingCollection = $this->workingCollection->filter(function($row) use ($value, $toSearch, $caseSensitive)
147+
$self = $this;
148+
$this->workingCollection = $this->workingCollection->filter(function($row) use ($value, $toSearch, $caseSensitive, $self)
147149
{
148150
for($i = 0; $i < count($row); $i++)
149151
{
150152
if(!in_array($i, $toSearch))
151153
continue;
152154

155+
$column = $i;
156+
if($self->aliasMapping)
157+
{
158+
$column = $self->getNameByIndex($i);
159+
}
160+
153161
if($this->options['stripSearch'])
154162
{
155-
$search = strip_tags($row[$i]);
163+
$search = strip_tags($row[$column]);
156164
}
157165
else
158166
{
159-
$search = $row[$i];
167+
$search = $row[$column];
160168
}
161169
if($caseSensitive)
162170
{
@@ -179,8 +187,13 @@ private function doInternalOrder()
179187

180188
$column = $this->orderColumn;
181189
$stripOrder = $this->options['stripOrder'];
182-
$this->workingCollection->sortBy(function($row) use ($column,$stripOrder) {
190+
$self = $this;
191+
$this->workingCollection->sortBy(function($row) use ($column,$stripOrder,$self) {
183192

193+
if($self->aliasMapping)
194+
{
195+
$column = $self->getNameByIndex($column);
196+
}
184197
if($stripOrder)
185198
{
186199
return strip_tags($row[$column]);
@@ -212,7 +225,15 @@ private function compileArray($columns)
212225
$i=0;
213226
foreach ($columns as $col)
214227
{
215-
$entry[$i] = $col->run($row);
228+
if($this->aliasMapping)
229+
{
230+
$entry[$col->getName()] = $col->run($row);
231+
}
232+
else
233+
{
234+
$entry[$i] = $col->run($row);
235+
}
236+
216237
$i++;
217238
}
218239
return $entry;

src/Chumper/Datatable/Engines/QueryEngine.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Chumper\Datatable\Engines;
22

3+
use Chumper\Datatable\Datatable;
34
use Illuminate\Database\Eloquent\Builder;
45
use Illuminate\Database\Eloquent\Relations\Relation;
56
use Illuminate\Support\Collection;
@@ -73,12 +74,14 @@ public function getArray()
7374
public function reset()
7475
{
7576
$this->builder = $this->originalBuilder;
77+
return $this;
7678
}
7779

7880

7981
public function setSearchOperator($value = "LIKE")
8082
{
8183
$this->options['searchOperator'] = $value;
84+
return $this;
8285
}
8386

8487
public function setSearchWithAlias()
@@ -194,7 +197,14 @@ private function compile($builder, $columns)
194197
$i = 0;
195198
foreach ($columns as $col)
196199
{
197-
$entry[$i] = $col->run($row);
200+
if($this->aliasMapping)
201+
{
202+
$entry[$col->getName()] = $col->run($row);
203+
}
204+
else
205+
{
206+
$entry[$i] = $col->run($row);
207+
}
198208
$i++;
199209
}
200210
return $entry;
@@ -204,7 +214,7 @@ private function compile($builder, $columns)
204214

205215
private function doInternalOrder($builder, $columns)
206216
{
207-
if(!$this->orderColumn == null)
217+
if(!is_null($this->orderColumn))
208218
{
209219
$i = 0;
210220
foreach($columns as $col)

0 commit comments

Comments
 (0)