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: 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 2d1c421..8ca37a6 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; @@ -9,38 +10,47 @@ 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 { - /** - * Build the html builder. - * - * @throws \Exception - */ - public function handle(): Builder + protected string $tableId = 'DummyTableId'; + + public function options(Builder $builder): void { - return $this->getHtmlBuilder() - ->setTableId('DummyTableId') - ->columns($this->getColumns()) - ->minifiedAjax() + $builder ->orderBy(1) - ->selectStyleSingle() - ->buttons([ - DummyButtons - ]); + ->languageSearchPlaceholder('Search...'); } /** - * Get the dataTable columns definition. + * @return array */ - public function getColumns(): array + public function columns(): array { return [ - Column::computed('action') - ->exportable(false) - ->printable(false) - ->width(60) - ->addClass('text-center'), DummyColumns ]; } + + /** + * @return array + */ + public function buttons(): array + { + return [ + DummyButtons + ]; + } + + /** + * @return array + */ + public function editors(): array + { + return [ +// Editor::make() +// ->fields([ +// Fields\Text::make('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 []; + } }