Skip to content

Commit ea4b44a

Browse files
authored
Merge pull request #2475 from yajra/search-panes
[9.0] Add support for search panes.
2 parents 0fbc3ba + 643df8a commit ea4b44a

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/DataTableAbstract.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable
145145
*/
146146
protected $serializer;
147147

148+
/**
149+
* @var array
150+
*/
151+
protected $searchPanes = [];
152+
148153
/**
149154
* Can the DataTable engine be created with these parameters.
150155
*
@@ -657,9 +662,18 @@ protected function filterRecords()
657662
}
658663

659664
$this->columnSearch();
665+
$this->searchPanesSearch();
660666
$this->filteredRecords = $this->isFilterApplied ? $this->filteredCount() : $this->totalRecords;
661667
}
662668

669+
/**
670+
* Perform search using search pane values.
671+
*/
672+
protected function searchPanesSearch()
673+
{
674+
// Add support for search pane.
675+
}
676+
663677
/**
664678
* Perform global search.
665679
*
@@ -772,6 +786,10 @@ protected function render(array $data)
772786
$output = $this->showDebugger($output);
773787
}
774788

789+
foreach ($this->searchPanes as $column => $searchPane) {
790+
$output['searchPanes']['options'][$column] = $searchPane['options'];
791+
}
792+
775793
return new JsonResponse(
776794
$output,
777795
200,
@@ -924,4 +942,26 @@ protected function getPrimaryKeyName()
924942
{
925943
return 'id';
926944
}
945+
946+
/**
947+
* Add a search pane options on response.
948+
*
949+
* @param string $column
950+
* @param mixed $options
951+
* @param callable|null $builder
952+
* @return $this
953+
*/
954+
public function searchPane($column, $options, callable $builder = null)
955+
{
956+
$options = value($options);
957+
958+
if ($options instanceof Arrayable) {
959+
$options = $options->toArray();
960+
}
961+
962+
$this->searchPanes[$column]['options'] = $options;
963+
$this->searchPanes[$column]['builder'] = $builder;
964+
965+
return $this;
966+
}
927967
}

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
*/

tests/Integration/QueryDataTableTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yajra\DataTables\DataTables;
1010
use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
1111
use Yajra\DataTables\QueryDataTable;
12+
use Yajra\DataTables\Tests\Models\User;
1213
use Yajra\DataTables\Tests\TestCase;
1314

1415
class QueryDataTableTest extends TestCase
@@ -229,6 +230,43 @@ public function it_allows_search_on_added_column_with_custom_filter_handler()
229230
$this->assertStringContainsString('"1" = ?', $queries[1]['query']);
230231
}
231232

233+
/** @test */
234+
public function it_returns_search_panes_options()
235+
{
236+
$crawler = $this->call('GET', '/query/search-panes');
237+
238+
$crawler->assertJson([
239+
'draw' => 0,
240+
'recordsTotal' => 20,
241+
'recordsFiltered' => 20,
242+
'searchPanes' => [
243+
'options' => [
244+
'name' => [],
245+
],
246+
],
247+
]);
248+
249+
$options = $crawler->json()['searchPanes']['options'];
250+
251+
$this->assertEquals(count($options['name']), 20);
252+
}
253+
254+
/** @test */
255+
public function it_performs_search_using_search_panes()
256+
{
257+
$crawler = $this->call('GET', '/query/search-panes', [
258+
'searchPanes' => [
259+
'id' => [1, 2],
260+
],
261+
]);
262+
263+
$crawler->assertJson([
264+
'draw' => 0,
265+
'recordsTotal' => 20,
266+
'recordsFiltered' => 2,
267+
]);
268+
}
269+
232270
/** @test */
233271
public function it_allows_column_search_added_column_with_custom_filter_handler()
234272
{
@@ -328,5 +366,13 @@ protected function setUp(): void
328366
->rawColumns(['name', 'email'])
329367
->toJson();
330368
});
369+
370+
$route->get('/query/search-panes', function (DataTables $dataTable) {
371+
$options = User::select('id as value', 'name as label')->get();
372+
373+
return $dataTable->query(DB::table('users'))
374+
->searchPane('name', $options)
375+
->toJson();
376+
});
331377
}
332378
}

0 commit comments

Comments
 (0)