Browse Source

修复switchgroup没有颜色默认值问题

jqh 5 years ago
parent
commit
1d6ab32c87
2 changed files with 15 additions and 57 deletions
  1. 3 46
      src/Grid/Displayers/SwitchDisplay.php
  2. 12 11
      src/Grid/Displayers/SwitchGroup.php

+ 3 - 46
src/Grid/Displayers/SwitchDisplay.php

@@ -14,46 +14,9 @@ class SwitchDisplay extends AbstractDisplayer
      */
     protected $color;
 
-    public function green()
-    {
-        $this->color = Admin::color()->success();
-    }
-
-    public function custom()
-    {
-        $this->color = Admin::color()->custom();
-    }
-
-    public function yellow()
-    {
-        $this->color = Admin::color()->warning();
-    }
-
-    public function red()
-    {
-        $this->color = Admin::color()->danger();
-    }
-
-    public function purple()
-    {
-        $this->color = Admin::color()->purple();
-    }
-
-    public function blue()
-    {
-        $this->color = Admin::color()->blue();
-    }
-
-    /**
-     * Set color of the switcher.
-     *
-     * @param string $color
-     *
-     * @return $this
-     */
     public function color($color)
     {
-        $this->color = $color;
+        $this->color = Admin::color()->get($color);
     }
 
     public function display(string $color = '')
@@ -61,19 +24,13 @@ class SwitchDisplay extends AbstractDisplayer
         if ($color instanceof \Closure) {
             $color->call($this->row, $this);
         } else {
-            if ($color) {
-                if (method_exists($this, $color)) {
-                    $this->$color();
-                } else {
-                    $this->color($color);
-                }
-            }
+            $this->color($color);
         }
 
         $this->setupScript();
 
         $name = $this->getElementName();
-        $key = $this->row->{$this->grid->getKeyName()};
+        $key = $this->getKey();
         $checked = $this->value ? 'checked' : '';
         $color = $this->color ?: Admin::color()->primary();
 

+ 12 - 11
src/Grid/Displayers/SwitchGroup.php

@@ -14,11 +14,7 @@ class SwitchGroup extends SwitchDisplay
         }
 
         if ($color) {
-            if (method_exists($this, $color)) {
-                $this->$color();
-            } else {
-                $this->color($color);
-            }
+            $this->color($color);
         }
 
         if (! Arr::isAssoc($columns)) {
@@ -61,10 +57,14 @@ class SwitchGroup extends SwitchDisplay
     } 
     init();
     swt.off('change').change(function(e) {
-        var t = $(this), id=t.data('key'),checked = t.is(':checked'), name = t.attr('name'), data = {
-            _token: Dcat.token,
-            _method: 'PUT'
-        };
+        var t = $(this), 
+            id = t.data('key'),
+            checked = t.is(':checked'), 
+            name = t.attr('name'), 
+            data = {
+                _token: Dcat.token,
+                _method: 'PUT'
+            };
         data[name] = checked ? 1 : 0;
         Dcat.NP.start();
     
@@ -86,13 +86,14 @@ class SwitchGroup extends SwitchDisplay
 JS;
         Admin::script($script);
 
-        $key = $this->row->{$this->grid->getKeyName()};
+        $key = $this->getKey();
         $checked = $this->row->$name ? 'checked' : '';
+        $color = $this->color ?: Admin::color()->primary();
 
         return <<<EOT
 <tr style="box-shadow: none;background: transparent">
     <td style="padding: 3px 0;height:23px;">{$label}:&nbsp;&nbsp;&nbsp;</td>
-    <td style="padding: 3px 0;height:23px;"><input name="{$elementName}" data-key="$key" $checked type="checkbox" class="$class" data-size="small" data-color="{$this->color}"/></td>
+    <td style="padding: 3px 0;height:23px;"><input name="{$elementName}" data-key="$key" $checked type="checkbox" class="$class" data-size="small" data-color="{$color}"/></td>
 </tr>
 EOT;
     }