summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2023-11-05 19:38:00 +0000
committerSam Light <samlight1994@gmail.com>2023-11-05 19:38:00 +0000
commit5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c (patch)
tree3de215fe85096429e615127172a2cee0b56661b0
parentbb6795b604e5686ee069c48dd7f7ce8cc3bf73c3 (diff)
Built basic table
-rw-r--r--resources/views/column.blade.php3
-rw-r--r--resources/views/table.blade.php28
-rw-r--r--src/Columns/Column.php68
-rw-r--r--src/Service.php8
-rw-r--r--src/ServiceProvider.php27
-rw-r--r--src/TableComponent.php55
6 files changed, 189 insertions, 0 deletions
diff --git a/resources/views/column.blade.php b/resources/views/column.blade.php
new file mode 100644
index 0000000..71f604d
--- /dev/null
+++ b/resources/views/column.blade.php
@@ -0,0 +1,3 @@
+<td {{ $attributes }} >
+ {{ $content }}
+</td>
diff --git a/resources/views/table.blade.php b/resources/views/table.blade.php
new file mode 100644
index 0000000..1938b48
--- /dev/null
+++ b/resources/views/table.blade.php
@@ -0,0 +1,28 @@
+<div>
+ <table class="table">
+ <thead>
+ <tr>
+ @foreach($columns as $column)
+ <th>{{ $column->title }}</th>
+ @endforeach
+ </tr>
+ </thead>
+ <tbody>
+ @foreach($data as $row)
+ <tr>
+ @foreach($columns as $column)
+ {{ $column->view($row) }}
+ @endforeach
+ </tr>
+ @endforeach
+ </tbody>
+ </table>
+ <div class="table-pagination d-flex justify-content-between align-items-center">
+ {{ $data->links() }}
+ <div>
+ {{ __('Showing') }} {{ $data->count() }}
+ {{ __('of') }} {{ $data->total() }}
+
+ </div>
+ </div>
+</div>
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 @@
+<?php
+
+namespace Lightscale\LaralightTables\Columns;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\View\ComponentAttributeBag;
+
+use Closure;
+
+class Column {
+
+ private TableComponent $table;
+
+ private Closure $displayFn;
+ private ?Closure $sortFn = null;
+ private ?Closure $searchFn = null;
+ private ?Closure $attributesFn = null;
+
+ public function __construct(
+ public string $name,
+ public string $title
+ ) {
+ $this->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 @@
+<?php
+
+namespace Lightscale\LaralightTables;
+
+class Service
+{
+ public const NAMESPACE = 'laralight-tables';
+}
diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php
new file mode 100644
index 0000000..40eb36f
--- /dev/null
+++ b/src/ServiceProvider.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Lightscale\LaralightTables;
+
+use Illuminate\Support\ServiceProvider as ServiceProviderBase;
+use Illuminate\Support\Facades\Blade;
+use Illuminate\Foundation\Console\AboutCommand;
+
+class ServiceProvider extends ServiceProviderBase
+{
+ public function register() : void
+ {
+
+ }
+
+ public function boot() : void
+ {
+ AboutCommand::add('Laralight Tables', fn() => [
+ '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 @@
+<?php
+
+namespace Lightscale\LaralightTables;
+
+use Livewire\Component;
+use Livewire\WithPagination;
+
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Model;
+
+use Exception;
+
+abstract class TableComponent extends Component
+{
+ use WithPagination;
+
+ protected $paginationTheme = 'bootstrap';
+ protected bool $searchable = true;
+
+ protected Model $model = null;
+
+ protected function query() : Builder
+ {
+ if($this->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'
+ ));
+ }
+}