Explorar el Código

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

jqh hace 5 años
padre
commit
97b5d98749
Se han modificado 1 ficheros con 35 adiciones y 2 borrados
  1. 35 2
      src/Show/Field.php

+ 35 - 2
src/Show/Field.php

@@ -335,6 +335,27 @@ HTML;
         });
     }
 
+    /**
+     * Add a `dot` before column text.
+     *
+     * @param array  $options
+     * @param string $default
+     *
+     * @return $this
+     */
+    public function dot($options = [], $default = 'default')
+    {
+        return $this->unescape()->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;";
+        });
+    }
+
     /**
      * Show field as badges.
      *
@@ -400,7 +421,13 @@ HTML;
      */
     public function prepend($val)
     {
-        return $this->as(function ($v) use (&$val) {
+        $name = $this->name;
+
+        return $this->as(function ($v) use (&$val, $name) {
+            if ($val instanceof \Closure) {
+                $val = $val->call($this, $v, $this->$name);
+            }
+
             if (is_array($v)) {
                 array_unshift($v, $val);
 
@@ -420,7 +447,13 @@ HTML;
      */
     public function append($val)
     {
-        return $this->as(function ($v) use (&$val) {
+        $name = $this->name;
+
+        return $this->as(function ($v) use (&$val, $name) {
+            if ($val instanceof \Closure) {
+                $val = $val->call($this, $v, $this->$name);
+            }
+
             if (is_array($v)) {
                 array_push($v, $val);