File tree Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Expand file tree Collapse file tree 4 files changed +60
-2
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 44
55use Illuminate \Foundation \Testing \DatabaseTransactions ;
66use Illuminate \Http \Response ;
7+ use Illuminate \Routing \Router ;
78use Illuminate \Support \Collection ;
89use PHPUnit \Framework \Attributes \Test ;
910use 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 ();
Original file line number Diff line number Diff line change 1010
1111class 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}
Original file line number Diff line number Diff line change 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 () } }
You can’t perform that action at this time.
0 commit comments