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
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ parameters:
- identifier: binaryOp.invalid
- identifier: encapsedStringPart.nonString
- identifier: return.missing
- identifier: offsetAccess.invalidOffset

excludePaths:
2 changes: 1 addition & 1 deletion src/Generators/DataTablesMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
54 changes: 32 additions & 22 deletions src/Generators/stubs/html.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,55 @@

namespace DummyNamespace;

use Illuminate\Support\Facades\Request;
use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
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<int, \Yajra\DataTables\Html\Column>
*/
public function getColumns(): array
public function columns(): array
{
return [
Column::computed('action')
->exportable(false)
->printable(false)
->width(60)
->addClass('text-center'),
DummyColumns
];
}

/**
* @return array<int, \Yajra\DataTables\Html\Button>
*/
public function buttons(): array
{
return [
DummyButtons
];
}

/**
* @return array<int, \Yajra\DataTables\Html\Editor\Editor>
*/
public function editors(): array
{
return [
// Editor::make()
// ->fields([
// Fields\Text::make('field_name'),
// ]),
];
}
}
78 changes: 68 additions & 10 deletions src/Html/DataTableHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -53,4 +77,38 @@ public function setHtmlBuilder(Builder $builder): static

return $this;
}

/**
* @return array{url: string, data: array<string, string>}|string
*/
public function ajax(): array|string
{
return Request::url();
}

public function options(Builder $builder): void {}

/**
* @return array<int, \Yajra\DataTables\Html\Column>
*/
public function columns(): array
{
return [];
}

/**
* @return array<int, \Yajra\DataTables\Html\Button>
*/
public function buttons(): array
{
return [];
}

/**
* @return array<int, \Yajra\DataTables\Html\Editor\Editor>
*/
public function editors(): array
{
return [];
}
}