56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Extensions\Show;
|
|
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Show\AbstractField;
|
|
use Dcat\Admin\Support\Helper;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class Label extends AbstractField
|
|
{
|
|
// 这个属性设置为false则不会转义HTML代码
|
|
public $escape = false;
|
|
|
|
public function render($style = 'primary', $max = null)
|
|
{
|
|
if (! $value = $this->value($max)) {
|
|
return;
|
|
}
|
|
|
|
return collect($value)->map(function ($name) use ($style) {
|
|
$background = $this->formatStyle(Arr::get($style, $name, 'default'));
|
|
return "<span class='label' {$background}>$name</span>";
|
|
})->implode(' ');
|
|
|
|
// return collect($value)->map(function ($name) use ($class, $background) {
|
|
// return "<span class='label bg-{$class}' {$background}>$name</span>";
|
|
// })->implode(' ');
|
|
}
|
|
|
|
protected function formatStyle($style)
|
|
{
|
|
$background = 'style="background:#d2d6de;color: #555"';
|
|
|
|
if ($style !== 'default') {
|
|
$style = Admin::color()->get($style);
|
|
|
|
$background = "style='background:{$style}'";
|
|
}
|
|
|
|
return $background;
|
|
}
|
|
|
|
protected function value($max)
|
|
{
|
|
$values = Helper::array($this->value);
|
|
|
|
if ($max && count($values) > $max) {
|
|
$values = array_slice($values, 0, $max);
|
|
$values[] = '...';
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
}
|