Browse Source

修复同个页面无法同时渲染多个异步组件问题

jqh 4 years ago
parent
commit
993a84d729
3 changed files with 17 additions and 4 deletions
  1. 2 1
      src/Widgets/Lazy.php
  2. 10 2
      src/Widgets/LazyTable.php
  3. 5 1
      src/Widgets/Widget.php

+ 2 - 1
src/Widgets/Lazy.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Dcat\Admin\Contracts\LazyRenderable;
 use Dcat\Admin\Traits\InteractsWithRenderApi;
+use Illuminate\Support\Str;
 
 class Lazy extends Widget
 {
@@ -17,7 +18,7 @@ class Lazy extends Widget
         $this->setRenderable($renderable);
         $this->load($load);
 
-        $this->class($this->elementClass = 'lazy-box');
+        $this->class(['lazy-box', $this->elementClass = 'lazy-'.Str::random(8)]);
     }
 
     /**

+ 10 - 2
src/Widgets/LazyTable.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Widgets;
 
 use Dcat\Admin\Grid\LazyRenderable;
+use Illuminate\Support\Str;
 
 class LazyTable extends Widget
 {
@@ -29,6 +30,11 @@ class LazyTable extends Widget
      */
     protected $simple;
 
+    /**
+     * @var string
+     */
+    protected $loadScript = '';
+
     /**
      * LazyTable constructor.
      *
@@ -40,7 +46,7 @@ class LazyTable extends Widget
         $this->from($renderable);
         $this->load($load);
 
-        $this->class($this->elementClass = 'async-table');
+        $this->class(['async-table', $this->elementClass = 'async-table-'.Str::random(8)]);
     }
 
     /**
@@ -106,7 +112,7 @@ class LazyTable extends Widget
      */
     public function onLoad(string $script)
     {
-        $this->script .= "\$this.on('table:loaded', function (event) { {$script} });";
+        $this->loadScript .= "\$this.on('table:loaded', function (event) { {$script} });";
 
         return $this;
     }
@@ -117,6 +123,8 @@ class LazyTable extends Widget
 Dcat.init('{$this->getElementSelector()}', function (\$this) {
     Dcat.grid.AsyncTable({container: \$this})
 
+    {$this->loadScript}
+
     {$this->getLoadScript()}
 });
 JS;

+ 5 - 1
src/Widgets/Widget.php

@@ -14,7 +14,7 @@ use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
 
 /**
- * @method $this class(string $class, bool $append = false)
+ * @method $this class(array|string $class, bool $append = false)
  * @method $this style(string $style, bool $append = true)
  * @method $this id(string $id = null)
  */
@@ -313,6 +313,10 @@ abstract class Widget implements Renderable
         if ($method === 'style' || $method === 'class') {
             $value = $parameters[0] ?? null;
             $append = $parameters[1] ?? ($method === 'class' ? false : true);
+            
+            if (is_array($value)) {
+                $value = implode(' ', $value);
+            }
 
             if ($append) {
                 $original = $this->htmlAttributes[$method] ?? '';