Bladeren bron

selectTable支持NestedForm

jqh 5 jaren geleden
bovenliggende
commit
ac46361a1d

+ 2 - 2
src/Form/Field/MultipleSelectTable.php

@@ -38,8 +38,8 @@ class MultipleSelectTable extends SelectTable
     {
         $this->script .= <<<JS
 Dcat.grid.SelectTable({
-    modal: replaceNestedFormIndex('#{$this->modal->getId()}'),
-    container: '{$this->getElementClassSelector()}',
+    modal: replaceNestedFormIndex('#{$this->modal->id()}'),
+    container: replaceNestedFormIndex('#{$this->getAttribute('id')}'),
     input: replaceNestedFormIndex('#hidden-{$this->id}'),
     button: replaceNestedFormIndex('#{$this->getButtonId()}'),
     multiple: true,

+ 24 - 11
src/Form/Field/SelectTable.php

@@ -105,11 +105,11 @@ class SelectTable extends Field
     protected function addScript()
     {
         $this->script .= <<<JS
-Dcat.grid.SelectTable({
-    modal: replaceNestedFormIndex('#{$this->modal->getId()}'),
-    container: '{$this->getElementClassSelector()}',
-    input: replaceNestedFormIndex('#hidden-{$this->id}'),
-    button: replaceNestedFormIndex('#{$this->getButtonId()}'),
+Dcat.grid.SelectTable({
+    modal: replaceNestedFormIndex('#{$this->modal->id()}'),
+    container: replaceNestedFormIndex('#{$this->getAttribute('id')}'),
+    input: replaceNestedFormIndex('#hidden-{$this->id}'),
+    button: replaceNestedFormIndex('#{$this->getButtonId()}'),
     values: {$this->options},
 });
 JS;
@@ -117,8 +117,10 @@ JS;
 
     protected function setUpModal()
     {
-        $this->modal
-            ->getTable()
+        $table = $this->modal->getTable();
+
+        $table
+            ->id('table-card-'.$this->getElementId())
             ->getRenderable()
             ->with('key', $this->form->getKey());
 
@@ -127,6 +129,16 @@ JS;
             ->id($this->getElementId())
             ->runScript(false)
             ->footer($this->renderFooter());
+
+        // 显示弹窗的时候异步加载表格
+        $this->modal->getModal()->onShow(
+            <<<JS
+if (! modal.table) {
+    modal.table = $(replaceNestedFormIndex('{$table->getElementSelector()}'));
+}            
+modal.table.trigger('table:load');
+JS
+        );
     }
 
     public function render()
@@ -139,7 +151,8 @@ JS;
         $this->prepend('<i class="feather icon-arrow-up"></i>')
             ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
             ->defaultAttribute('type', 'text')
-            ->defaultAttribute('name', $name);
+            ->defaultAttribute('name', $name)
+            ->defaultAttribute('id', 'container-'.$this->getElementId());
 
         $this->addVariables([
             'prepend'     => $this->prepend,
@@ -152,7 +165,7 @@ JS;
         $this->script = $this->modal->getScript();
 
         $this->addScript();
-
+//        dd($this->script);
         return parent::render();
     }
 
@@ -167,7 +180,7 @@ JS;
         $cancel = trans('admin.cancel');
 
         return <<<HTML
-<a id="{$this->getButtonId()}" class="btn btn-primary" style="color: #fff">&nbsp;{$submit}&nbsp;</a>&nbsp;
+<a id="{$this->getButtonId()}" class="btn btn-primary" style="color: #fff">&nbsp;{$submit}&nbsp;</a>&nbsp;
 <a onclick="$(this).parents('.modal').modal('toggle')" class="btn btn-white">&nbsp;{$cancel}&nbsp;</a>
 HTML;
     }
@@ -177,7 +190,7 @@ HTML;
      *
      * @return string
      */
-    protected function getButtonId()
+    public function getButtonId()
     {
         return 'submit-'.$this->id;
     }

+ 4 - 5
src/Widgets/AsyncTable.php

@@ -56,10 +56,9 @@ class AsyncTable extends Widget
 
     protected function addScript()
     {
-        Admin::script(<<<'JS'
-Dcat.grid.AsyncTable('.table-card');
-JS
-        );
+        $this->script = <<<JS
+Dcat.grid.AsyncTable({container: replaceNestedFormIndex('{$this->getElementSelector()}')});
+JS;
 
         if ($this->load) {
             $this->script .= $this->getLoadScript();
@@ -80,7 +79,7 @@ JS
     public function getLoadScript()
     {
         return <<<JS
-$(replaceNestedFormIndex('{$this->getElementSelector()}')).trigger('table:load');
+$('{$this->getElementSelector()}').trigger('table:load');
 JS;
     }
 

+ 2 - 10
src/Widgets/Modal.php

@@ -296,14 +296,6 @@ class Modal extends Widget
         return $this;
     }
 
-    /**
-     * @return string
-     */
-    public function getId()
-    {
-        return $this->getHtmlAttribute('id');
-    }
-
     /**
      * 获取弹窗元素选择器.
      *
@@ -311,7 +303,7 @@ class Modal extends Widget
      */
     public function getElementSelector()
     {
-        return '#'.$this->getId();
+        return '#'.$this->id();
     }
 
     protected function addEventScript()
@@ -436,7 +428,7 @@ HTML;
         }
 
         return <<<HTML
-<span style="cursor: pointer" data-toggle="modal" data-target="#{$this->getId()}">{$button}</span>
+<span style="cursor: pointer" data-toggle="modal" data-target="#{$this->id()}">{$button}</span>
 HTML;
     }
 }

+ 5 - 17
src/Widgets/Sparkline/Sparkline.php

@@ -87,6 +87,8 @@ class Sparkline extends Widget
         $this->values($values);
 
         $this->options['type'] = $this->type;
+
+        $this->id($this->generateId());
     }
 
     /**
@@ -161,7 +163,7 @@ class Sparkline extends Widget
 
         if (! $this->allowBuildRequest()) {
             return $this->script = <<<JS
-$('#{$this->getId()}').sparkline($values, $options);
+$('#{$this->id()}').sparkline($values, $options);
 {$this->buildCombinationScript()};
 JS;
         }
@@ -171,7 +173,7 @@ JS;
 if (!response.status) {
     return Dcat.error(response.message || 'Server internal error.');
 }        
-var id = '{$this->getId()}', opt = $options;
+var id = '{$this->id()}', opt = $options;
 opt = $.extend(opt, response.options || {});
 $('#'+id).sparkline(response.values || $values, opt);
 JS
@@ -192,7 +194,7 @@ JS
             $options = json_encode($value[1]);
 
             $script .= <<<JS
-$('#{$this->getId()}').sparkline($value, $options);
+$('#{$this->id()}').sparkline($value, $options);
 JS;
         }
 
@@ -206,10 +208,6 @@ JS;
     {
         $this->addScript();
 
-        $this->setHtmlAttribute([
-            'id' => $this->getId(),
-        ]);
-
         return parent::render();
     }
 
@@ -220,16 +218,6 @@ JS;
 HTML;
     }
 
-    /**
-     * 获取容器元素ID.
-     *
-     * @return string
-     */
-    public function getId()
-    {
-        return $this->id ?: ($this->id = $this->generateId());
-    }
-
     /**
      * @param string $method
      * @param array  $parameters

+ 24 - 6
src/Widgets/TableModal.php

@@ -18,7 +18,6 @@ use Illuminate\Contracts\Support\Renderable;
  * @method $this onHidden(string $script)
  * @method $this onHide(string $script)
  * @method $this footer(string|\Closure|Renderable $footer)
- * @method $this getId()
  * @method $this getElementSelector()
  */
 class TableModal extends Widget
@@ -47,7 +46,6 @@ class TableModal extends Widget
         'onShow',
         'onHidden',
         'onHide',
-        'getId',
         'getElementSelector',
         'footer',
     ];
@@ -88,9 +86,25 @@ class TableModal extends Widget
 
         $this->table = AsyncTable::make($renderable, false);
 
-        $this->modal
-            ->body($this->table)
-            ->onShow($this->table->getLoadScript());
+        $this->modal->body($this->table);
+
+        return $this;
+    }
+
+    /**
+     * 设置或获取ID.
+     *
+     * @param string|null $id
+     *
+     * @return |string
+     */
+    public function id(string $id = null)
+    {
+        $result = $this->modal->id($id);
+
+        if ($id === null) {
+            return $result;
+        }
 
         return $this;
     }
@@ -114,6 +128,10 @@ class TableModal extends Widget
      */
     public function html()
     {
+        if ($this->runScript) {
+            $this->modal->onShow($this->table->getLoadScript());
+        }
+
         if ($this->loadScript) {
             $this->table->onLoad($this->loadScript);
         }
@@ -160,7 +178,7 @@ class TableModal extends Widget
         if (in_array($method, $this->allowMethods, true)) {
             $result = $this->modal->$method(...$parameters);
 
-            if (in_array($method, ['getElementSelector', 'getId'], true)) {
+            if (in_array($method, ['getElementSelector'], true)) {
                 return $result;
             }
 

+ 1 - 1
src/Widgets/Widget.php

@@ -268,7 +268,7 @@ abstract class Widget implements Renderable
         }
 
         // 获取属性
-        if (count($parameters) === 0) {
+        if (count($parameters) === 0 || $parameters[0] === null) {
             return $this->getHtmlAttribute($method);
         }