From 5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c Mon Sep 17 00:00:00 2001 From: Sam Light Date: Sun, 5 Nov 2023 19:38:00 +0000 Subject: Built basic table --- src/Columns/Column.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Service.php | 8 ++++++ src/ServiceProvider.php | 27 ++++++++++++++++++++ src/TableComponent.php | 55 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 src/Columns/Column.php create mode 100644 src/Service.php create mode 100644 src/ServiceProvider.php create mode 100644 src/TableComponent.php (limited to 'src') diff --git a/src/Columns/Column.php b/src/Columns/Column.php new file mode 100644 index 0000000..7b9f248 --- /dev/null +++ b/src/Columns/Column.php @@ -0,0 +1,68 @@ +displayFn = Closure::fromCallable([$this, 'defaultDisplay']); + } + + public static function make(string $name, $title) : static + { + return new static($name, $title); + } + + private function defaultDisplay(Model $row, Column $column) + { + return $row->{$column->name}; + } + + public function display(callable $fn) : static + { + $this->displayFn = Closure::fromCallable($fn); + return $this; + } + + public function sortable(callable $fn) : static + { + $this->sortFn = Closure::fromCallable($fn); + return $this; + } + + public function searchable(callable $fn) : static + { + $this->searchFn = Closure::fromCallable($fn); + return $this; + } + + public function attributes(callable $fn) : static + { + $this->attributesFn = Closure::fromCallable($fn); + return $this; + } + + public function view(Model $row) + { + $attributes = $this->attributesFn?->call($this, $row) ?? []; + $attributes = new ComponentAttributeBag($attributes); + $content = $this->displayFn->call($this, $row, $this); + return view('laralight-tables::column', compact('attributes', 'content')); + } + +} diff --git a/src/Service.php b/src/Service.php new file mode 100644 index 0000000..01d6180 --- /dev/null +++ b/src/Service.php @@ -0,0 +1,8 @@ + [ + 'Version' => 'dev' + ]); + + //$this->loadTranslationsFrom(__DIR__ . '/../lang', Service::NAMESPACE); + $this->loadViewsFrom(__DIR__ . '/../resources/views', Service::NAMESPACE); + Blade::componentNamespace('Lightscale\\LaralightCms\\View\\Components', Service::NAMESPACE); + } + +} diff --git a/src/TableComponent.php b/src/TableComponent.php new file mode 100644 index 0000000..e530415 --- /dev/null +++ b/src/TableComponent.php @@ -0,0 +1,55 @@ +model === null) { + throw new Exception('Requires $model to be set or query() method to be overridden'); + } + + return $this->model::query(); + } + + abstract protected function columns() : array; + + protected function search(Builder $builder, string $search) : void {} + + protected function filters() : array + { + return []; + } + + protected buildQuery() : Builder + { + $query = $this->query(); + } + + public function render() + { + $this->buildQuery()->paginate(); + $data = $query->paginate($this->pageSize); + $columns = $this->columns(); + + return view('laralight-tables::table', compact( + 'data', 'columns' + )); + } +} -- cgit v1.2.3