From 77ddf2e59d8b3562a2461b584655dff687209433 Mon Sep 17 00:00:00 2001 From: Ozan Kurt Date: Thu, 8 Aug 2024 06:47:24 +0200 Subject: [PATCH 1/2] Ability to use static data (no server-side) --- src/Html/Builder.php | 18 +++++++++ src/Html/Cell.php | 59 ++++++++++++++++++++++++++++ src/Html/HasOptions.php | 1 + src/Html/Options/HasRows.php | 60 +++++++++++++++++++++++++++++ src/Html/Row.php | 75 ++++++++++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+) create mode 100644 src/Html/Cell.php create mode 100644 src/Html/Options/HasRows.php create mode 100644 src/Html/Row.php diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 9172874..d5d604b 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -46,6 +46,11 @@ class Builder */ public Collection $collection; + /** + * @var Collection + */ + public Collection $rows; + /** * @var array */ @@ -68,6 +73,7 @@ public function __construct(public Repository $config, public Factory $view, pub $defaults = $this->config->get('datatables-html.table', []); $this->collection = new Collection; + $this->rows = new Collection; $this->tableAttributes = $defaults; } @@ -179,6 +185,18 @@ public function table(array $attributes = [], bool $drawFooter = false, bool $dr $tableHtml .= 'theadClass ?? '').'>'; $tableHtml .= ''.implode('', $th).''.$searchHtml.''; + if ($this->attributes['serverSide'] === false) { + $tableHtml .= ''; + foreach ($this->getRows() as $row) { + $tableHtml .= ''; + foreach ($this->getColumns() as $column) { + $tableHtml .= ''.$row->getCellContentForColumn($column).''; + } + $tableHtml .= ''; + } + $tableHtml .= ''; + } + if ($drawFooter) { $tf = $this->compileTableFooter(); $tableHtml .= ''.implode('', $tf).''; diff --git a/src/Html/Cell.php b/src/Html/Cell.php new file mode 100644 index 0000000..12e96e2 --- /dev/null +++ b/src/Html/Cell.php @@ -0,0 +1,59 @@ + $data, + 'content' => $content, + ]; + } + + return new static($attributes); + } + + /** + * @return $this + */ + public function column(string $value): static + { + $this->attributes['column'] = $value; + + return $this; + } + + /** + * @return $this + */ + public function content(string $value): static + { + $this->attributes['content'] = $value; + + return $this; + } +} diff --git a/src/Html/HasOptions.php b/src/Html/HasOptions.php index 14fcd6d..1a31507 100644 --- a/src/Html/HasOptions.php +++ b/src/Html/HasOptions.php @@ -12,6 +12,7 @@ trait HasOptions use Options\HasAjax; use Options\HasCallbacks; use Options\HasColumns; + use Options\HasRows; use Options\HasFeatures; use Options\HasInternationalisation; use Options\Plugins\AutoFill; diff --git a/src/Html/Options/HasRows.php b/src/Html/Options/HasRows.php new file mode 100644 index 0000000..b2a8d59 --- /dev/null +++ b/src/Html/Options/HasRows.php @@ -0,0 +1,60 @@ +rows = new Collection; + + foreach ($rows as $key => $value) { + if (! is_a($value, Row::class)) { + if (array_key_exists('cells', $value)) { + $cells = $value['cells']; + $attributes = $value['attributes'] ?? []; + } else { + $cells = $value; + $attributes = []; + } + + $this->rows->push(new Row($attributes, $cells)); + } else { + $this->rows->push($value); + } + } + + return $this; + } + + public function setRows(Collection|array $rows): static + { + $this->rows = collect($rows); + + return $this; + } + + /** + * Get collection of rows. + * + * @return \Illuminate\Support\Collection + */ + public function getRows(): Collection + { + return $this->rows; + } +} diff --git a/src/Html/Row.php b/src/Html/Row.php new file mode 100644 index 0000000..66981e8 --- /dev/null +++ b/src/Html/Row.php @@ -0,0 +1,75 @@ +buildCells($cells); + + parent::__construct($attributes); + } + + /** + * Make a new column instance. + */ + public static function make(array $attributes = [], array $cells = []): static + { + return new static($attributes, $cells); + } + + public function cells($cells): static + { + $this->cells = collect($cells); + + return $this; + } + + protected function buildCells(array $cells): static + { + $this->cells = new Collection; + + foreach ($cells as $key => $value) { + if (! is_a($value, Cell::class)) { + if (is_array($value)) { + $cellAttributes = array_merge($value, [ + 'column' => $value['column'] ?? $key, + 'content' => $value['content'] ?? null, + ]); + } else { + $cellAttributes = [ + 'column' => $key, + 'content' => $value, + ]; + } + + $this->cells->push(new Cell($cellAttributes)); + } else { + $this->cells->push($value); + } + } + + $this->cells = $this->cells->keyBy('column'); + + return $this; + } + + public function getCellContentForColumn(Column $column): mixed + { + if ($this->cells->has($column->data)) { + return $this->cells->get($column->data)->content; + } + + return null; + } +} From 0be35167a5ac31c32d2147b15ba7524ef5582d0c Mon Sep 17 00:00:00 2001 From: Ozan Kurt Date: Sun, 11 Aug 2024 05:27:21 +0200 Subject: [PATCH 2/2] Update src/Html/Builder.php Co-authored-by: Arjay Angeles --- src/Html/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Builder.php b/src/Html/Builder.php index d5d604b..38fa0f7 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -47,7 +47,7 @@ class Builder public Collection $collection; /** - * @var Collection + * @var Collection */ public Collection $rows;