From 312e9af37e9beb0eb4aa43a3b0e1d907ffb24d84 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 27 Sep 2025 13:25:45 +0800 Subject: [PATCH 1/5] feat: improve html builder --- src/Generators/stubs/html.stub | 55 +++++++++++++++--------- src/Html/DataTableHtml.php | 78 +++++++++++++++++++++++++++++----- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/src/Generators/stubs/html.stub b/src/Generators/stubs/html.stub index 2d1c421..27abf9f 100644 --- a/src/Generators/stubs/html.stub +++ b/src/Generators/stubs/html.stub @@ -2,6 +2,7 @@ namespace DummyNamespace; +use Illuminate\Support\Facades\Request; use Yajra\DataTables\Html\Builder; use Yajra\DataTables\Html\Button; use Yajra\DataTables\Html\Column; @@ -11,36 +12,48 @@ use Yajra\DataTables\Html\Editor\Editor; class DummyClass extends DataTableHtml { + protected string $tableId = 'DummyTableId'; + + public function options(Builder $builder): void + { + $builder + ->orderBy(1) + ->languageSearchPlaceholder('Search...'); + } + /** - * Build the html builder. - * - * @throws \Exception + * @return array */ - public function handle(): Builder + public function columns(): array { - return $this->getHtmlBuilder() - ->setTableId('DummyTableId') - ->columns($this->getColumns()) - ->minifiedAjax() - ->orderBy(1) - ->selectStyleSingle() - ->buttons([ - DummyButtons - ]); + return [ + Column::make('id'), + // Column::make('add_your_columns_here'), + Column::make('created_at'), + Column::make('updated_at'), + ]; + } + + /** + * @return array + */ + public function buttons(): array + { + return [ + // Button::make('create'), + ]; } /** - * Get the dataTable columns definition. + * @return array */ - public function getColumns(): array + public function editors(): array { return [ - Column::computed('action') - ->exportable(false) - ->printable(false) - ->width(60) - ->addClass('text-center'), - DummyColumns + // Editor::make() + // ->fields([ + // Fields::text('field_name'), + // ]), ]; } } diff --git a/src/Html/DataTableHtml.php b/src/Html/DataTableHtml.php index 92456f4..db73253 100644 --- a/src/Html/DataTableHtml.php +++ b/src/Html/DataTableHtml.php @@ -2,16 +2,18 @@ namespace Yajra\DataTables\Html; -use BadMethodCallException; +use Illuminate\Support\Facades\Request; +use Illuminate\Support\Traits\ForwardsCalls; use Yajra\DataTables\Contracts\DataTableHtmlBuilder; -/** - * @mixin Builder - */ abstract class DataTableHtml implements DataTableHtmlBuilder { + use ForwardsCalls; + protected ?Builder $htmlBuilder = null; + protected string $tableId = 'dataTable'; + public static function make(): Builder { if (func_get_args()) { @@ -31,11 +33,7 @@ public static function make(): Builder */ public function __call(string $method, mixed $parameters) { - if (method_exists($this->getHtmlBuilder(), $method)) { - return $this->getHtmlBuilder()->{$method}(...$parameters); - } - - throw new BadMethodCallException("Method {$method} does not exists"); + return $this->forwardCallTo($this->htmlBuilder ?? $this->getHtmlBuilder(), $method, $parameters); } protected function getHtmlBuilder(): Builder @@ -44,7 +42,33 @@ protected function getHtmlBuilder(): Builder return $this->htmlBuilder; } - return $this->htmlBuilder = app(Builder::class); + $this->htmlBuilder = app(Builder::class); + + $this->options($this->htmlBuilder); + + if ($this->buttons()) { + $this->htmlBuilder->buttons($this->buttons()); + } + + if ($this->columns()) { + $this->htmlBuilder->columns($this->columns()); + } + + if ($this->editors()) { + $this->htmlBuilder->editors($this->editors()); + } + + return $this->htmlBuilder; + } + + public function handle(): Builder + { + return $this->getHtmlBuilder() + ->setTableId($this->tableId) + ->selectSelector() + ->selectStyleOs() + ->postAjax($this->ajax()) + ->addScript('datatables::functions.batch_remove'); } public function setHtmlBuilder(Builder $builder): static @@ -53,4 +77,38 @@ public function setHtmlBuilder(Builder $builder): static return $this; } + + /** + * @return array{url: string, data: array}|string + */ + public function ajax(): array|string + { + return Request::url(); + } + + public function options(Builder $builder): void {} + + /** + * @return array + */ + public function columns(): array + { + return []; + } + + /** + * @return array + */ + public function buttons(): array + { + return []; + } + + /** + * @return array + */ + public function editors(): array + { + return []; + } } From ae8fe5c4c31fb76d41c5b376730a52f02a98d697 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 27 Sep 2025 13:28:23 +0800 Subject: [PATCH 2/5] chore: fix types --- phpstan.neon.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 7822d95..378419f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -22,5 +22,6 @@ parameters: - identifier: binaryOp.invalid - identifier: encapsedStringPart.nonString - identifier: return.missing + - identifier: offsetAccess.invalidOffset excludePaths: From 19efe361f1b95d7a77b997194679a9cb65ba51a9 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 27 Sep 2025 13:41:39 +0800 Subject: [PATCH 3/5] fix: columns and buttons dummy stub --- src/Generators/DataTablesMakeCommand.php | 2 +- src/Generators/stubs/html.stub | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Generators/DataTablesMakeCommand.php b/src/Generators/DataTablesMakeCommand.php index 0a96770..afad3d8 100644 --- a/src/Generators/DataTablesMakeCommand.php +++ b/src/Generators/DataTablesMakeCommand.php @@ -194,7 +194,7 @@ protected function getButtons(): string /** * Parse array from definition. */ - protected function parseButtons(string $definition, int $indentation = 24): string + protected function parseButtons(string $definition, int $indentation = 12): string { $columns = explode(',', $definition); $stub = ''; diff --git a/src/Generators/stubs/html.stub b/src/Generators/stubs/html.stub index 27abf9f..d574daa 100644 --- a/src/Generators/stubs/html.stub +++ b/src/Generators/stubs/html.stub @@ -27,10 +27,7 @@ class DummyClass extends DataTableHtml public function columns(): array { return [ - Column::make('id'), - // Column::make('add_your_columns_here'), - Column::make('created_at'), - Column::make('updated_at'), + DummyColumns ]; } @@ -40,7 +37,7 @@ class DummyClass extends DataTableHtml public function buttons(): array { return [ - // Button::make('create'), + DummyButtons ]; } From 1edcf2a02e49c259ec03c58a87fe82a09f53d869 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 27 Sep 2025 13:51:13 +0800 Subject: [PATCH 4/5] chore: make stub a final class --- src/Generators/stubs/html.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/stubs/html.stub b/src/Generators/stubs/html.stub index d574daa..0e25e30 100644 --- a/src/Generators/stubs/html.stub +++ b/src/Generators/stubs/html.stub @@ -10,7 +10,7 @@ use Yajra\DataTables\Html\DataTableHtml; use Yajra\DataTables\Html\Editor\Fields; use Yajra\DataTables\Html\Editor\Editor; -class DummyClass extends DataTableHtml +final class DummyClass extends DataTableHtml { protected string $tableId = 'DummyTableId'; From c5fd0a775c2d88bb6c5498711db2c321211a04e4 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 27 Sep 2025 13:52:55 +0800 Subject: [PATCH 5/5] chore: editors stub --- src/Generators/stubs/html.stub | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generators/stubs/html.stub b/src/Generators/stubs/html.stub index 0e25e30..8ca37a6 100644 --- a/src/Generators/stubs/html.stub +++ b/src/Generators/stubs/html.stub @@ -47,10 +47,10 @@ final class DummyClass extends DataTableHtml public function editors(): array { return [ - // Editor::make() - // ->fields([ - // Fields::text('field_name'), - // ]), +// Editor::make() +// ->fields([ +// Fields\Text::make('field_name'), +// ]), ]; } }