diff --git a/resources/views/layouts/table.blade.php b/resources/views/layouts/table.blade.php index 0cd225e2d..d86616419 100644 --- a/resources/views/layouts/table.blade.php +++ b/resources/views/layouts/table.blade.php @@ -35,7 +35,7 @@ @foreach($rows as $source) - + contextualClass())> @foreach($columns as $column) {!! $column->buildTd($source, $loop->parent) !!} @endforeach diff --git a/src/Screen/Layouts/Table.php b/src/Screen/Layouts/Table.php index cd7c69afe..39f0bde0b 100644 --- a/src/Screen/Layouts/Table.php +++ b/src/Screen/Layouts/Table.php @@ -7,10 +7,12 @@ use Illuminate\Contracts\Pagination\CursorPaginator; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Contracts\View\View; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Orchid\Screen\Layout; use Orchid\Screen\Repository; use Orchid\Screen\TD; +use Orchid\Support\Color; abstract class Table extends Layout { @@ -63,6 +65,7 @@ public function build(Repository $repository): ?View 'onEachSide' => $this->onEachSide(), 'showHeader' => $this->hasHeader($columns, $rows), 'title' => $this->title, + 'resolveRowColor' => fn($row) => $this->resolveRowColor($row), ]); } @@ -154,4 +157,13 @@ protected function total(): array { return []; } + + /** + * A method that processes a string and returns the color of the string. + */ + protected function resolveRowColor(Repository|Model|string $row): Color + { + return Color::DEFAULT; + } + } diff --git a/src/Support/Color.php b/src/Support/Color.php index 4849d0f29..c133c22eb 100644 --- a/src/Support/Color.php +++ b/src/Support/Color.php @@ -44,6 +44,26 @@ public function name(): string }; } + /** + * This method returns the bootstrap table color class. + * + * @return string + */ + public function contextualClass(): string + { + return match ($this) { + Color::INFO => 'table-info', + Color::SUCCESS => 'table-success', + Color::WARNING => 'table-warning', + Color::BASIC, Color::DEFAULT => '', + Color::DANGER, Color::ERROR => 'table-danger', + Color::PRIMARY, Color::LINK => 'table-primary', + Color::SECONDARY => 'table-secondary', + Color::LIGHT => 'table-light', + Color::DARK => 'table-dark', + }; + } + /** * This method returns the color based on the given name. * It is used to maintain backwards compatibility to 13.0. diff --git a/stubs/app/Orchid/Layouts/Examples/ResolveRowColorTableExample.php b/stubs/app/Orchid/Layouts/Examples/ResolveRowColorTableExample.php new file mode 100644 index 000000000..2b04f3be6 --- /dev/null +++ b/stubs/app/Orchid/Layouts/Examples/ResolveRowColorTableExample.php @@ -0,0 +1,68 @@ +width('100') + ->render(fn (Repository $model) => // Please use view('path') + "sample + # {$model->get('id')}"), + + TD::make('name', 'Name') + ->width('450') + ->render(fn (Repository $model) => Str::limit($model->get('name'), 200)), + + TD::make('price', 'Price') + ->width('100') + ->usingComponent(Currency::class, before: '$') + ->align(TD::ALIGN_RIGHT), + + TD::make('created_at', 'Created') + ->width('100') + ->usingComponent(DateTimeSplit::class) + ->align(TD::ALIGN_RIGHT), + ]; + } + + protected function resolveRowColor(Repository|Model|string $row): Color + { + return match (true) { + $row->get('price') > 700 => Color::DANGER, + $row->get('price') < 1 => Color::SUCCESS, + default => Color::DEFAULT, + }; + } + +} diff --git a/stubs/app/Orchid/Screens/Examples/ExampleScreen.php b/stubs/app/Orchid/Screens/Examples/ExampleScreen.php index 81a046934..3c0d1dd73 100644 --- a/stubs/app/Orchid/Screens/Examples/ExampleScreen.php +++ b/stubs/app/Orchid/Screens/Examples/ExampleScreen.php @@ -4,6 +4,7 @@ use App\Orchid\Layouts\Examples\ChartBarExample; use App\Orchid\Layouts\Examples\ChartLineExample; +use App\Orchid\Layouts\Examples\ResolveRowColorTableExample; use Illuminate\Http\Request; use Illuminate\Support\Str; use Orchid\Screen\Actions\Button; @@ -64,6 +65,14 @@ public function query(): iterable new Repository(['id' => 400, 'name' => self::TEXT_EXAMPLE, 'price' => 0.1, 'created_at' => '01.01.2020']), new Repository(['id' => 500, 'name' => self::TEXT_EXAMPLE, 'price' => 0.15, 'created_at' => '01.01.2020']), + ], + 'resolveRowColor' => [ + new Repository(['id' => 100, 'name' => self::TEXT_EXAMPLE, 'price' => 10.24, 'created_at' => '01.01.2020']), + new Repository(['id' => 200, 'name' => self::TEXT_EXAMPLE, 'price' => 65.9, 'created_at' => '01.01.2020']), + new Repository(['id' => 300, 'name' => self::TEXT_EXAMPLE, 'price' => 754.2, 'created_at' => '01.01.2020']), + new Repository(['id' => 400, 'name' => self::TEXT_EXAMPLE, 'price' => 0.1, 'created_at' => '01.01.2020']), + new Repository(['id' => 500, 'name' => self::TEXT_EXAMPLE, 'price' => 0.15, 'created_at' => '01.01.2020']), + ], 'metrics' => [ 'sales' => ['value' => number_format(6851), 'diff' => 10.08], @@ -157,6 +166,8 @@ class='mw-100 d-block img-fluid rounded-1 w-100'> ->align(TD::ALIGN_RIGHT), ]), + ResolveRowColorTableExample::class, + Layout::modal('exampleModal', Layout::rows([ Input::make('toast') ->title('Messages to display')