Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Services/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ abstract class DataTable implements DataTableButtons
*/
protected string $pdfWriter = 'Dompdf';

/**
* @phpstan-var view-string|null
*/
protected ?string $view = null;

public function __construct()
{
/** @var Request $request */
Expand All @@ -163,6 +168,11 @@ public function __construct()
$this->htmlBuilder = $builder;
}

public function __invoke(): mixed
{
return $this->render($this->view, $this->viewData(), $this->viewMergeData());
}

/**
* Process dataTables needed render output.
*
Expand Down Expand Up @@ -742,4 +752,20 @@ protected function buildFastExcelFile(): FastExcel

return (new FastExcel($dataTable->toArray()['data']))->setColumnStyles($styles);
}

/**
* @return array<string, mixed>
*/
protected function viewData(): array
{
return [];
}

/**
* @return array<string, mixed>
*/
protected function viewMergeData(): array
{
return [];
}
}
18 changes: 18 additions & 0 deletions tests/DataTableServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -95,6 +96,23 @@ public function it_is_macroable(): void
$this->assertEquals('macro', $dataTable->macroMethod());
}

#[Test]
public function it_can_be_used_as_route_action(): void
{
/** @var Router|null $router */
$router = $this->app['router'] ?? null;
$router?->get('datatables-as-route-action', UsersDataTable::class);

$this->get('datatables-as-route-action')
->assertSeeText('LaravelDataTables')
->assertSeeText('This is a test description');

// Assert that view data are not present when manually calling render
$this->get('users')
->assertSeeText('DataTable')
->assertSeeText('No description');
}

protected function setUp(): void
{
parent::setUp();
Expand Down
10 changes: 10 additions & 0 deletions tests/DataTables/UsersDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class UsersDataTable extends DataTable
{
protected ?string $view = 'tests::users';

public function dataTable(Builder $query): EloquentDataTable
{
return (new EloquentDataTable($query))
Expand All @@ -36,4 +38,12 @@ protected function filename(): string
{
return 'Users';
}

protected function viewData(): array
{
return [
'title' => 'LaravelDataTables',
'description' => 'This is a test description',
];
}
}
8 changes: 6 additions & 2 deletions tests/views/users.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{{ $dataTable->table() }}
<h2>{{ $title ?? 'DataTable' }}</h2>
<p>{{ $description ?? 'No description' }}</p>
<div>
{{ $dataTable->table() }}
</div>

{{ $dataTable->scripts() }}
{{ $dataTable->scripts() }}
Loading