Skip to content

Commit 255f016

Browse files
committed
Merge pull request bgultekin#57 from toddmcbrearty/master
Added clean columns function
2 parents de17305 + baf9281 commit 255f016

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

src/Bllim/Datatables/Datatables.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Datatables
2828
protected $edit_columns = array();
2929
protected $sColumns = array();
3030

31-
public $columns = array();
31+
public $columns = array();
3232
public $last_columns = array();
3333

34-
protected $count_all = 0;
34+
protected $count_all = 0;
3535

3636
protected $result_object;
3737
protected $result_array = array();
@@ -344,19 +344,48 @@ private function ordering()
344344

345345
if(!is_null(Input::get('iSortCol_0')))
346346
{
347+
$columns = $this->cleanColumns( $this->last_columns );
347348

348349
for ( $i=0, $c=intval(Input::get('iSortingCols')); $i<$c ; $i++ )
349350
{
350351
if ( Input::get('bSortable_'.intval(Input::get('iSortCol_'.$i))) == "true" )
351352
{
352-
if(isset($this->last_columns[intval(Input::get('iSortCol_'.$i))]))
353-
$this->query->orderBy($this->last_columns[intval(Input::get('iSortCol_'.$i))],Input::get('sSortDir_'.$i));
353+
if(isset($columns[intval(Input::get('iSortCol_'.$i))]))
354+
$this->query->orderBy($columns[intval(Input::get('iSortCol_'.$i))],Input::get('sSortDir_'.$i));
354355
}
355356
}
356357

357358
}
358359
}
359360

361+
private function cleanColumns( $cols )
362+
{
363+
$_search = [
364+
'GROUP_CONCAT( ',
365+
'CONCAT( ',
366+
'DISTINCT( ',
367+
',',
368+
' )',
369+
'as',
370+
];
371+
372+
foreach ( $cols as $col )
373+
{
374+
$_column = explode( ' ' , str_replace( $_search, '', $col, $count ) );
375+
376+
if ( $count > 0 )
377+
{
378+
$columns[] = array_shift( $_column );
379+
}
380+
else
381+
{
382+
$columns[] = end( $_column );
383+
}
384+
}
385+
386+
return $columns;
387+
}
388+
360389
/**
361390
* Datatable filtering
362391
*
@@ -365,11 +394,12 @@ private function ordering()
365394

366395
private function filtering()
367396
{
368-
397+
$columns = $this->cleanColumns( $this->columns );
369398

370399
if (Input::get('sSearch','') != '')
371400
{
372401
$copy_this = $this;
402+
$copy_this->columns = $columns;
373403

374404
$this->query->where(function($query) use ($copy_this) {
375405

@@ -418,7 +448,7 @@ private function filtering()
418448

419449
$db_prefix = $this->database_prefix();
420450

421-
for ($i=0,$c=count($this->columns);$i<$c;$i++)
451+
for ($i=0,$c=count($columns);$i<$c;$i++)
422452
{
423453
if (Input::get('bSearchable_'.$i) == "true" && Input::get('sSearch_'.$i) != '')
424454
{
@@ -429,10 +459,10 @@ private function filtering()
429459
}
430460

431461
if(Config::get('datatables.search.case_insensitive', false)) {
432-
$column = $db_prefix . $this->columns[$i];
462+
$column = $db_prefix . $columns[$i];
433463
$this->query->where(DB::raw('LOWER('.$column.')'),'LIKE', $keyword);
434464
} else {
435-
$this->query->where($this->columns[$i], 'LIKE', $keyword);
465+
$this->query->where($columns[$i], 'LIKE', $keyword);
436466
}
437467
}
438468
}

0 commit comments

Comments
 (0)