Browse Source

Grid\Column::prepand、Grid\Column::append方法增加支持闭包类型参数;增加Grid\Column::dot方法

jqh 5 years ago
parent
commit
943aab5871
2 changed files with 37 additions and 11 deletions
  1. 31 2
      src/Grid/Column/HasDisplayers.php
  2. 6 9
      src/Grid/Displayers/Label.php

+ 31 - 2
src/Grid/Column/HasDisplayers.php

@@ -118,7 +118,11 @@ trait HasDisplayers
      */
     public function prepend($val)
     {
-        return $this->display(function ($v) use (&$val) {
+        return $this->display(function ($v, $column) use (&$val) {
+            if ($val instanceof \Closure) {
+                $val = $val->call($this, $v, $column->getOriginal(), $column);
+            }
+
             if (is_array($v)) {
                 array_unshift($v, $val);
 
@@ -138,7 +142,11 @@ trait HasDisplayers
      */
     public function append($val)
     {
-        return $this->display(function ($v) use (&$val) {
+        return $this->display(function ($v, $column) use (&$val) {
+            if ($val instanceof \Closure) {
+                $val = $val->call($this, $v, $column->getOriginal(), $column);
+            }
+
             if (is_array($v)) {
                 array_push($v, $val);
 
@@ -189,6 +197,27 @@ trait HasDisplayers
         });
     }
 
+    /**
+     * Add a `dot` before column text.
+     *
+     * @param array  $options
+     * @param string $default
+     *
+     * @return $this
+     */
+    public function dot($options = [], $default = 'default')
+    {
+        return $this->prepend(function ($_, $original) use ($options, $default) {
+            $style = is_null($original) ? $default : Arr::get((array) $options, $original, $default);
+
+            $style = $style === 'default' ? 'dark70' : $style;
+
+            $background = Admin::color()->get($style);
+
+            return "<i class='fa fa-circle' style='font-size: 13px;color: {$background}'></i>&nbsp;&nbsp;";
+        });
+    }
+
     /**
      * Limit the number of characters in a string, or the number of element in a array.
      *

+ 6 - 9
src/Grid/Displayers/Label.php

@@ -8,7 +8,6 @@ use Dcat\Admin\Support\Helper;
 class Label extends AbstractDisplayer
 {
     protected $baseClass = 'label';
-    protected $stylePrefix = 'bg';
 
     public function display($style = 'primary', $max = null)
     {
@@ -19,30 +18,28 @@ class Label extends AbstractDisplayer
         $original = $this->column->getOriginal();
         $defaultStyle = is_array($style) ? ($style['default'] ?? 'default') : 'default';
 
-        [$class, $background] = $this->formatStyle(
+        $background= $this->formatStyle(
             is_array($style) ?
                 (is_scalar($original) ? ($style[$original] ?? $defaultStyle) : current($style))
                 : $style
         );
 
-        return collect($value)->map(function ($name) use ($class, $background) {
-            return "<span class='{$this->baseClass} {$this->stylePrefix}-{$class}' {$background}>$name</span>";
+        return collect($value)->map(function ($name) use ($background) {
+            return "<span class='{$this->baseClass}' {$background}>$name</span>";
         })->implode('&nbsp;');
     }
 
     protected function formatStyle($style)
     {
-        $class = 'default';
-        $background = '';
+        $background = '#d2d6de';
 
         if ($style !== 'default') {
-            $class = '';
-
             $style = Admin::color()->get($style);
+
             $background = "style='background:{$style}'";
         }
 
-        return [$class, $background];
+        return $background;
     }
 
     protected function value($max)