Переглянути джерело

行内编辑增加保存成功后刷新页面参数

jqh 4 роки тому
батько
коміт
6b4c566b55

+ 5 - 5
src/Grid/Column.php

@@ -17,19 +17,19 @@ use Illuminate\Support\Traits\Macroable;
 
 /**
  * @method $this editable(bool $refresh = false)
- * @method $this switch(string $color = '')
- * @method $this switchGroup($columns = [], string $color = '')
+ * @method $this switch(string $color = '', $refresh = false)
+ * @method $this switchGroup($columns = [], string $color = '', $refresh = false)
  * @method $this image($server = '', int $width = 200, int $height = 200)
  * @method $this label($style = 'primary', int $max = null)
  * @method $this button($style = 'success');
  * @method $this link($href = '', $target = '_blank');
  * @method $this badge($style = 'primary', int $max = null);
  * @method $this progressBar($style = 'primary', $size = 'sm', $max = 100)
- * @method $this checkbox($options = [])
- * @method $this radio($options = [])
+ * @method $this checkbox($options = [], $refresh = false)
+ * @method $this radio($options = [], $refresh = false)
  * @method $this expand($callbackOrButton = null)
  * @method $this table($titles = [])
- * @method $this select($options = [])
+ * @method $this select($options = [], $refresh = false)
  * @method $this modal($title = '', $callback = null)
  * @method $this showTreeInDialog($callbackOrNodes = null)
  * @method $this qrcode($formatter = null, $width = 150, $height = 150)

+ 11 - 10
src/Grid/Displayers/Checkbox.php

@@ -7,7 +7,7 @@ use Dcat\Admin\Support\Helper;
 
 class Checkbox extends AbstractDisplayer
 {
-    public function display($options = [])
+    public function display($options = [], $refresh = false)
     {
         if ($options instanceof \Closure) {
             $options = $options->call($this, $this->row);
@@ -34,7 +34,7 @@ class Checkbox extends AbstractDisplayer
 EOT;
         }
 
-        Admin::script($this->script());
+        Admin::script($this->addScript($refresh));
 
         return <<<EOT
 <form class="form-group {$this->getElementClass()}" style="text-align:left;" data-key="{$this->getKey()}">
@@ -54,19 +54,19 @@ EOT;
         return 'grid-checkbox-'.$this->column->getName();
     }
 
-    protected function script()
+    protected function addScript($refresh)
     {
         return <<<JS
 (function () {
-    var f;
     $('form.{$this->getElementClass()}').off('submit').on('submit', function () {
         var values = $(this).find('input:checkbox:checked').map(function (_, el) {
             return $(el).val();
-        }).get(), btn = $(this).find('[type="submit"]');
-        
-        if (f) return;
-        f = 1;
+        }).get(), 
+        btn = $(this).find('[type="submit"]'),
+        reload = '{$refresh}';
         
+        if (btn.attr('loading')) return;
+        btn.attr('loading', 1);
         btn.buttonLoading();
     
         var data = {
@@ -82,12 +82,13 @@ EOT;
             data: JSON.stringify(data),
             success: function (data) {
                 btn.buttonLoading(false);
-                f = 0;
+                btn.attr('loading', 0);
                 Dcat.success(data.message);
+                reload && Dcat.reload();
             },
             error: function (a, b, c) {
                 btn.buttonLoading(false);
-                f = 0;
+                btn.attr('loading', 0);
                 Dcat.handleAjaxError(a, b, c);
             },
         });

+ 13 - 10
src/Grid/Displayers/Radio.php

@@ -6,7 +6,7 @@ use Dcat\Admin\Admin;
 
 class Radio extends AbstractDisplayer
 {
-    public function display($options = [])
+    public function display($options = [], $refresh = false)
     {
         if ($options instanceof \Closure) {
             $options = $options->call($this, $this->row);
@@ -30,7 +30,7 @@ class Radio extends AbstractDisplayer
 EOT;
         }
 
-        Admin::script($this->script());
+        Admin::script($this->addScript($refresh));
 
         return <<<EOT
 <form class="form-group {$this->getElementClass()}" style="text-align: left" data-key="{$this->getKey()}">
@@ -50,17 +50,19 @@ EOT;
         return 'grid-radio-'.$this->column->getName();
     }
 
-    protected function script()
+    protected function addScript($refresh)
     {
         return <<<JS
 (function () {
-    var f;
     $('form.{$this->getElementClass()}').on('submit', function () {
-        var value = $(this).find('input:radio:checked').val(), btn = $(this).find('[type="submit"]');
-        
-        if (f) return;
-        f = 1;
+        var value = $(this).find('input:radio:checked').val(), 
+            btn = $(this).find('[type="submit"]'),
+            reload = '{$refresh}';
         
+        if (btn.attr('loading')) {
+            return;
+        }
+        btn.attr('loading', 1);
         btn.buttonLoading();
     
         $.ajax({
@@ -73,12 +75,13 @@ EOT;
             },
             success: function (data) {
                 btn.buttonLoading(false);
-                f = 0;
+                btn.attr('loading', 0);
                 Dcat.success(data.message);
+                reload && Dcat.reload()
             },
             error: function (a, b, c) {
                 btn.buttonLoading(false);
-                f = 0;
+                btn.attr('loading', 0);
                 Dcat.handleAjaxError(a, b, c);
             },
         });

+ 6 - 4
src/Grid/Displayers/Select.php

@@ -11,13 +11,13 @@ class Select extends AbstractDisplayer
 
     protected $selector = 'grid-column-select';
 
-    public function display($options = [])
+    public function display($options = [], $refresh = false)
     {
         if ($options instanceof \Closure) {
             $options = $options->call($this, $this->row);
         }
 
-        $this->addScript();
+        $this->addScript($refresh);
 
         $optionsHtml = '';
 
@@ -40,7 +40,7 @@ EOT;
         return $this->resource().'/'.$this->getKey();
     }
 
-    protected function addScript()
+    protected function addScript($refresh)
     {
         $script = <<<JS
 $('.{$this->selector}').off('change').select2().on('change', function(){
@@ -50,7 +50,8 @@ $('.{$this->selector}').off('change').select2().on('change', function(){
         data = {
             _token: Dcat.token,
             _method: 'PUT'
-        };
+        },
+        reload = '{$refresh}';
     
     if (name.indexOf('.') === -1) {
         data[name] = value;
@@ -69,6 +70,7 @@ $('.{$this->selector}').off('change').select2().on('change', function(){
         success: function (data) {
             Dcat.NP.done();
             Dcat.success(data.message);
+            reload && Dcat.reload();
         }
     });
 });

+ 11 - 7
src/Grid/Displayers/SwitchDisplay.php

@@ -21,7 +21,7 @@ class SwitchDisplay extends AbstractDisplayer
         $this->color = Admin::color()->get($color);
     }
 
-    public function display(string $color = '')
+    public function display(string $color = '', $refresh = false)
     {
         if ($color instanceof \Closure) {
             $color->call($this->row, $this);
@@ -29,7 +29,7 @@ class SwitchDisplay extends AbstractDisplayer
             $this->color($color);
         }
 
-        $this->addScript();
+        $this->addScript($refresh);
 
         $checked = $this->value ? 'checked' : '';
         $color = $this->color ?: Admin::color()->primary();
@@ -44,20 +44,23 @@ EOF;
         return $this->resource().'/'.$this->getKey();
     }
 
-    protected function addScript()
+    protected function addScript($refresh)
     {
         Admin::script(
             <<<JS
-(function(){
-    var swt = $('.{$this->selector}'), that;
-    function init(){
+(function() {
+    var swt = $('.{$this->selector}'), 
+    that, 
+    reload = '{$refresh}';
+    function initSwitchery(){
         swt.parent().find('.switchery').remove();
         swt.each(function(k){
             that = $(this);
             new Switchery(that[0], that.data())
         })
     } 
-    init();
+    initSwitchery();
+    
     swt.off('change').change(function(e) {
         var that = $(this), 
          url = that.data('url'),
@@ -88,6 +91,7 @@ EOF;
                 Dcat.NP.done();
                 if (d.status) {
                     Dcat.success(d.message);
+                    reload && Dcat.reload();
                 } else {
                     Dcat.error(d.message);
                 }

+ 10 - 7
src/Grid/Displayers/SwitchGroup.php

@@ -9,13 +9,13 @@ class SwitchGroup extends SwitchDisplay
 {
     protected $selector = 'grid-column-switch-group';
 
-    public function display($columns = [], string $color = '')
+    public function display($columns = [], string $color = '', $refresh = false)
     {
         if ($columns instanceof \Closure) {
             $columns = $columns->call($this->row, $this);
         }
 
-        $this->addScript();
+        $this->addScript($refresh);
 
         if ($color) {
             $this->color($color);
@@ -49,20 +49,22 @@ class SwitchGroup extends SwitchDisplay
 EOT;
     }
 
-    protected function addScript()
+    protected function addScript($refresh)
     {
         $script = <<<JS
 (function () {
-    var swt = $('.{$this->selector}'), that;
-    function init(){
-        swt.each(function(){
+    var swt = $('.{$this->selector}'),
+        reload = '{$refresh}', 
+        that;
+    function initSwitchery() {
+        swt.each(function() {
              that = $(this);
              that.parent().find('.switchery').remove();
              
              new Switchery(that[0], that.data())
         })
     } 
-    init();
+    initSwitchery();
     swt.off('change').change(function(e) {
         var that = $(this), 
             id = that.data('key'),
@@ -93,6 +95,7 @@ EOT;
                 Dcat.NP.done();
                  if (d.status) {
                     Dcat.success(d.message);
+                    reload && Dcat.reload()
                 } else {
                     Dcat.error(d.message);
                 }