Skip to content

Commit

Permalink
Merge pull request #2475 from yajra/search-panes
Browse files Browse the repository at this point in the history
[9.0] Add support for search panes.
  • Loading branch information
yajra authored Oct 31, 2020
2 parents 0fbc3ba + 643df8a commit ea4b44a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable
*/
protected $serializer;

/**
* @var array
*/
protected $searchPanes = [];

/**
* Can the DataTable engine be created with these parameters.
*
Expand Down Expand Up @@ -657,9 +662,18 @@ protected function filterRecords()
}

$this->columnSearch();
$this->searchPanesSearch();
$this->filteredRecords = $this->isFilterApplied ? $this->filteredCount() : $this->totalRecords;
}

/**
* Perform search using search pane values.
*/
protected function searchPanesSearch()
{
// Add support for search pane.
}

/**
* Perform global search.
*
Expand Down Expand Up @@ -772,6 +786,10 @@ protected function render(array $data)
$output = $this->showDebugger($output);
}

foreach ($this->searchPanes as $column => $searchPane) {
$output['searchPanes']['options'][$column] = $searchPane['options'];
}

return new JsonResponse(
$output,
200,
Expand Down Expand Up @@ -924,4 +942,26 @@ protected function getPrimaryKeyName()
{
return 'id';
}

/**
* Add a search pane options on response.
*
* @param string $column
* @param mixed $options
* @param callable|null $builder
* @return $this
*/
public function searchPane($column, $options, callable $builder = null)
{
$options = value($options);

if ($options instanceof Arrayable) {
$options = $options->toArray();
}

$this->searchPanes[$column]['options'] = $options;
$this->searchPanes[$column]['builder'] = $builder;

return $this;
}
}
22 changes: 22 additions & 0 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ public function make($mDataSupport = true)
}
}

/**
* Perform search using search pane values.
*/
protected function searchPanesSearch()
{
$columns = $this->request->get('searchPanes', []);

foreach ($columns as $column => $values) {
if ($this->isBlacklisted($column)) {
continue;
}

if ($callback = data_get($this->searchPanes, $column . '.builder')) {
$callback($this->getBaseQueryBuilder(), $values);
} else {
$this->getBaseQueryBuilder()->whereIn($column, $values);
}

$this->isFilterApplied = true;
}
}

/**
* Prepare query by executing count, filter, order and paginate.
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/Integration/QueryDataTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Yajra\DataTables\DataTables;
use Yajra\DataTables\Facades\DataTables as DatatablesFacade;
use Yajra\DataTables\QueryDataTable;
use Yajra\DataTables\Tests\Models\User;
use Yajra\DataTables\Tests\TestCase;

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

/** @test */
public function it_returns_search_panes_options()
{
$crawler = $this->call('GET', '/query/search-panes');

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 20,
'searchPanes' => [
'options' => [
'name' => [],
],
],
]);

$options = $crawler->json()['searchPanes']['options'];

$this->assertEquals(count($options['name']), 20);
}

/** @test */
public function it_performs_search_using_search_panes()
{
$crawler = $this->call('GET', '/query/search-panes', [
'searchPanes' => [
'id' => [1, 2],
],
]);

$crawler->assertJson([
'draw' => 0,
'recordsTotal' => 20,
'recordsFiltered' => 2,
]);
}

/** @test */
public function it_allows_column_search_added_column_with_custom_filter_handler()
{
Expand Down Expand Up @@ -328,5 +366,13 @@ protected function setUp(): void
->rawColumns(['name', 'email'])
->toJson();
});

$route->get('/query/search-panes', function (DataTables $dataTable) {
$options = User::select('id as value', 'name as label')->get();

return $dataTable->query(DB::table('users'))
->searchPane('name', $options)
->toJson();
});
}
}

0 comments on commit ea4b44a

Please sign in to comment.