Skip to content

Commit 6e2e0c3

Browse files
feat: datatable as route action
1 parent a68e9db commit 6e2e0c3

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/Services/DataTable.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ abstract class DataTable implements DataTableButtons
151151
*/
152152
protected string $pdfWriter = 'Dompdf';
153153

154+
/**
155+
* @phpstan-var view-string|null
156+
*/
157+
protected ?string $view = null;
158+
154159
public function __construct()
155160
{
156161
/** @var Request $request */
@@ -163,6 +168,11 @@ public function __construct()
163168
$this->htmlBuilder = $builder;
164169
}
165170

171+
public function __invoke(): mixed
172+
{
173+
return $this->render($this->view, $this->viewData(), $this->viewMergeData());
174+
}
175+
166176
/**
167177
* Process dataTables needed render output.
168178
*
@@ -742,4 +752,20 @@ protected function buildFastExcelFile(): FastExcel
742752

743753
return (new FastExcel($dataTable->toArray()['data']))->setColumnStyles($styles);
744754
}
755+
756+
/**
757+
* @return array<string, mixed>
758+
*/
759+
protected function viewData(): array
760+
{
761+
return [];
762+
}
763+
764+
/**
765+
* @return array<string, mixed>
766+
*/
767+
protected function viewMergeData(): array
768+
{
769+
return [];
770+
}
745771
}

tests/DataTableServiceTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Foundation\Testing\DatabaseTransactions;
66
use Illuminate\Http\Response;
7+
use Illuminate\Routing\Router;
78
use Illuminate\Support\Collection;
89
use PHPUnit\Framework\Attributes\Test;
910
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -95,6 +96,23 @@ public function it_is_macroable(): void
9596
$this->assertEquals('macro', $dataTable->macroMethod());
9697
}
9798

99+
#[Test]
100+
public function it_can_be_used_as_route_action(): void
101+
{
102+
/** @var Router|null $router */
103+
$router = $this->app['router'] ?? null;
104+
$router?->get('datatables-as-route-action', UsersDataTable::class);
105+
106+
$this->get('datatables-as-route-action')
107+
->assertSeeText('LaravelDataTables')
108+
->assertSeeText('This is a test description');
109+
110+
// Assert that view data are not present when manually calling render
111+
$this->get('users')
112+
->assertSeeText('DataTable')
113+
->assertSeeText('No description');
114+
}
115+
98116
protected function setUp(): void
99117
{
100118
parent::setUp();

tests/DataTables/UsersDataTable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class UsersDataTable extends DataTable
1212
{
13+
protected ?string $view = 'tests::users';
14+
1315
public function dataTable(Builder $query): EloquentDataTable
1416
{
1517
return (new EloquentDataTable($query))
@@ -36,4 +38,12 @@ protected function filename(): string
3638
{
3739
return 'Users';
3840
}
41+
42+
protected function viewData(): array
43+
{
44+
return [
45+
'title' => 'LaravelDataTables',
46+
'description' => 'This is a test description',
47+
];
48+
}
3949
}

tests/views/users.blade.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
{{ $dataTable->table() }}
1+
<h2>{{ $title ?? 'DataTable' }}</h2>
2+
<p>{{ $description ?? 'No description' }}</p>
3+
<div>
4+
{{ $dataTable->table() }}
5+
</div>
26

3-
{{ $dataTable->scripts() }}
7+
{{ $dataTable->scripts() }}

0 commit comments

Comments
 (0)