Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added color row for tables #2950

Open
wants to merge 4 commits into
base: 15.0-dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion resources/views/layouts/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<tbody>

@foreach($rows as $source)
<tr>
<tr @class($resolveRowColor($source)->contextualClass())>
@foreach($columns as $column)
{!! $column->buildTd($source, $loop->parent) !!}
@endforeach
Expand Down
12 changes: 12 additions & 0 deletions src/Screen/Layouts/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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),
]);
}

Expand Down Expand Up @@ -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;
}

}
20 changes: 20 additions & 0 deletions src/Support/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions stubs/app/Orchid/Layouts/Examples/ResolveRowColorTableExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Orchid\Layouts\Examples;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Orchid\Screen\Components\Cells\Currency;
use Orchid\Screen\Components\Cells\DateTimeSplit;
use Orchid\Screen\Layouts\Table;
use Orchid\Screen\Repository;
use Orchid\Screen\TD;
use Orchid\Support\Color;

class ResolveRowColorTableExample extends Table
{
/**
* Data source.
*
* The name of the key to fetch it from the query.
* The results of which will be elements of the table.
*
* @var string
*/
protected string $target = 'resolveRowColor';

/**
* Get the table cells to be displayed.
*
* @return TD[]
* @throws \ReflectionException
*/
protected function columns(): iterable
{
return [
TD::make('id', 'ID')
->width('100')
->render(fn (Repository $model) => // Please use view('path')
"<img src='https://loremflickr.com/500/300?random={$model->get('id')}'
alt='sample'
class='mw-100 d-block img-fluid rounded-1 w-100'>
<span class='small text-muted mt-1 mb-0'># {$model->get('id')}</span>"),

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,
};
}

}
11 changes: 11 additions & 0 deletions stubs/app/Orchid/Screens/Examples/ExampleScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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')
Expand Down
Loading