Skip to content

Commit fec72d8

Browse files
authored
Merge pull request yajra#847 from lmhaydi/L4-develop
[4.2] Add override auto filter and add ordering by nulls last.
2 parents eff5e45 + c02cb08 commit fec72d8

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

src/config/config.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
'search' => [
66
'case_insensitive' => true,
77
'use_wildcards' => false,
8+
],
9+
10+
'order' => [
11+
//first %s column, second %s order direction
12+
'nulls_last_sql' => '%s %s NULLS LAST',
813
]
914

1015
];

src/yajra/Datatables/Datatables.php

+60-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ class Datatables
143143
*/
144144
protected $autoFilter = true;
145145

146+
/**
147+
* Flag for ordering NULLS LAST option.
148+
*
149+
* @var boolean
150+
*/
151+
protected $nullsLast = false;
152+
146153
/**
147154
* Flag for DT version
148155
*
@@ -345,6 +352,13 @@ private function count()
345352
->setBindings($bindings)->count();
346353
}
347354

355+
public function orderByNullsLast()
356+
{
357+
$this->nullsLast = true;
358+
359+
return $this;
360+
}
361+
348362
/**
349363
* Organizes works
350364
*
@@ -356,7 +370,7 @@ public function make($mDataSupport = false)
356370
// set mData support flag
357371
$this->mDataSupport = $mDataSupport;
358372

359-
// check if auto filtering was overidden
373+
// check if auto filtering was overridden
360374
if ($this->autoFilter) {
361375
$this->doFiltering();
362376
}
@@ -528,6 +542,31 @@ public function isCaseInsensitive()
528542
return Config::get('datatables::search.case_insensitive', false);
529543
}
530544

545+
/**
546+
* Get config ordering nulls last
547+
*
548+
* @return boolean
549+
*/
550+
public function isNullsLast()
551+
{
552+
return $this->nullsLast;
553+
}
554+
555+
/**
556+
* Get NULLS LAST SQL.
557+
*
558+
* @param $column
559+
* @param $direction
560+
* @return string
561+
*/
562+
private function getNullsLastSql($column, $direction)
563+
{
564+
$sql = Config::get('datatables::order.nulls_last_sql', '%s %s NULLS LAST');
565+
566+
return sprintf($sql, $column, $direction);
567+
}
568+
569+
531570
/**
532571
* Clean columns name
533572
*
@@ -687,17 +726,31 @@ private function doOrdering()
687726
$column = $this->input['columns'][$order_col];
688727
if ($column['orderable'] == "true") {
689728
if (! empty($column['name'])) {
690-
$this->query->orderBy($column['name'], $order_dir);
729+
if($this->isNullsLast()){
730+
$this->query->orderByRaw($this->getNullsLastSql($column['name'], $order_dir));
731+
732+
}else{
733+
$this->query->orderBy($column['name'], $order_dir);
734+
}
691735
} elseif (isset($this->columns[$order_col])) {
692736
$column_name = $this->getColumnName($this->columns[$order_col]);
693-
$this->query->orderBy($column_name, $order_dir);
737+
if($this->isNullsLast()){
738+
$this->query->orderByRaw($this->getNullsLastSql($column_name, $order_dir));
739+
}else{
740+
$this->query->orderBy($column_name, $order_dir);
741+
}
742+
694743
}
695744
}
696745
} else {
697746
if (isset($this->columns[$order_col])) {
698747
if ($this->input['columns'][$order_col]['orderable'] == "true") {
699748
$column_name = $this->getColumnName($this->columns[$order_col]);
700-
$this->query->orderBy($column_name, $order_dir);
749+
if($this->isNullsLast()){
750+
$this->query->orderByRaw($this->getNullsLastSql($column_name, $order_dir));
751+
}else{
752+
$this->query->orderBy($column_name, $order_dir);
753+
}
701754
}
702755
}
703756
}
@@ -987,12 +1040,13 @@ public function removeColumn()
9871040
* Set auto filter off and run your own filter
9881041
*
9891042
* @param callable $callback
1043+
* @param $disable_auto_filter
9901044
* @return Datatables
9911045
* @internal param $Closure
9921046
*/
993-
public function filter(Closure $callback)
1047+
public function filter(Closure $callback, $disable_auto_filter = false)
9941048
{
995-
$this->autoFilter = false;
1049+
$this->autoFilter = $disable_auto_filter;
9961050

9971051
$query = $this->query;
9981052
call_user_func($callback, $query);

0 commit comments

Comments
 (0)