Skip to content

Commit 6c5e797

Browse files
committed
Add search pane custom search handler.
1 parent 34931b6 commit 6c5e797

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/CollectionDataTable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ protected function globalSearch($keyword)
250250
});
251251
}
252252

253+
/**
254+
* Perform search using search pane values.
255+
*/
256+
protected function searchPanesSearch()
257+
{
258+
throw new \Exception('Search panes is not yet supported on collection');
259+
}
260+
253261
/**
254262
* Perform default query orderBy clause.
255263
*/

src/DataTableAbstract.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -668,22 +668,9 @@ protected function filterRecords()
668668
}
669669

670670
/**
671-
* Perform search on submitted search panes.
671+
* Perform search using search pane values.
672672
*/
673-
protected function searchPanesSearch()
674-
{
675-
$columns = $this->request->get('searchPanes', []);
676-
677-
foreach ($columns as $column => $values) {
678-
if ($this->isBlacklisted($column) && ! $this->hasFilterColumn($column)) {
679-
continue;
680-
}
681-
682-
$this->getBaseQueryBuilder()->whereIn($column, $values);
683-
684-
$this->isFilterApplied = true;
685-
}
686-
}
673+
abstract protected function searchPanesSearch();
687674

688675
/**
689676
* Perform global search.
@@ -797,8 +784,8 @@ protected function render(array $data)
797784
$output = $this->showDebugger($output);
798785
}
799786

800-
foreach ($this->searchPanes as $column => $options) {
801-
$output['searchPanes']['options'][$column] = $options;
787+
foreach ($this->searchPanes as $column => $searchPane) {
788+
$output['searchPanes']['options'][$column] = $searchPane['options'];
802789
}
803790

804791
return new JsonResponse(
@@ -957,17 +944,19 @@ protected function getPrimaryKeyName()
957944
/**
958945
* @param string $column
959946
* @param mixed $options
947+
* @param \Closure|null $builder
960948
* @return $this
961949
*/
962-
public function searchPanes($column, $options)
950+
public function searchPanes($column, $options, callable $builder = null)
963951
{
964952
if ($options instanceof Arrayable) {
965953
$options = $options->toArray();
966954
} else {
967955
$options = value($options);
968956
}
969957

970-
$this->searchPanes[$column] = $options;
958+
$this->searchPanes[$column]['options'] = $options;
959+
$this->searchPanes[$column]['builder'] = $builder;
971960

972961
return $this;
973962
}

src/QueryDataTable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ public function make($mDataSupport = true)
107107
}
108108
}
109109

110+
/**
111+
* Perform search using search pane values.
112+
*/
113+
protected function searchPanesSearch()
114+
{
115+
$columns = $this->request->get('searchPanes', []);
116+
117+
foreach ($columns as $column => $values) {
118+
if ($this->isBlacklisted($column)) {
119+
continue;
120+
}
121+
122+
if ($callback = data_get($this->searchPanes, $column . '.builder')) {
123+
$callback($this->getBaseQueryBuilder(), $values);
124+
} else {
125+
$this->getBaseQueryBuilder()->whereIn($column, $values);
126+
}
127+
128+
$this->isFilterApplied = true;
129+
}
130+
}
131+
110132
/**
111133
* Prepare query by executing count, filter, order and paginate.
112134
*/

0 commit comments

Comments
 (0)