Browse Source

优化表格事件代码

jqh 4 years ago
parent
commit
833c9121bc

+ 3 - 10
src/Form/Field/Button.php

@@ -12,20 +12,13 @@ class Button extends Field
     public function __construct($label)
     {
         parent::__construct(Str::random(), [$label]);
-    }
-
-    public function class(string $class)
-    {
-        $this->class = $class;
 
-        return $this;
+        $this->addVariables(['buttonClass' => $this->class]);
     }
 
-    public function variables()
+    public function class(string $class)
     {
-        $this->addVariables(['buttonClass' => $this->class]);
-
-        return parent::variables();
+        return $this->addVariables(['buttonClass' => $class]);
     }
 
     public function on($event, $callback)

+ 2 - 2
src/Form/NestedForm.php

@@ -63,8 +63,8 @@ class NestedForm extends WidgetForm
 
         $this->key = $key;
 
-        $this->disableResetButton();
-        $this->disableSubmitButton();
+        $this->resetButton(false);
+        $this->submitButton(false);
         $this->ajax(false);
         $this->useFormTag(false);
     }

+ 2 - 0
src/Grid/Concerns/HasEvents.php

@@ -39,6 +39,8 @@ trait HasEvents
     {
         $this->dispatched[get_class($event)] = $event;
 
+        $event->setGrid($this);
+
         Event::dispatch($event);
     }
 

+ 1 - 1
src/Grid/Concerns/HasExporter.php

@@ -75,7 +75,7 @@ trait HasExporter
 
         $this->callBuilder();
 
-        $this->fire(new Grid\Events\Exporting($this, [$scope]));
+        $this->fire(new Grid\Events\Exporting([$scope]));
 
         // clear output buffer.
         if (ob_get_length()) {

+ 0 - 2
src/Grid/Concerns/HasFilter.php

@@ -37,8 +37,6 @@ trait HasFilter
         $this->callBuilder();
         $this->handleExportRequest();
 
-        $this->fireOnce(new Grid\Events\Fetching($this));
-
         $this->applyQuickSearch();
         $this->applyColumnFilter();
         $this->applySelectorQuery();

+ 1 - 1
src/Grid/Concerns/HasQuickSearch.php

@@ -89,7 +89,7 @@ trait HasQuickSearch
             return;
         }
 
-        $this->fireOnce(new ApplyQuickSearch($this, [$query]));
+        $this->fireOnce(new ApplyQuickSearch([$query]));
 
         // 树表格子节点忽略查询条件
         $this->model()

+ 1 - 1
src/Grid/Concerns/HasSelector.php

@@ -58,7 +58,7 @@ trait HasSelector
                 return;
             }
 
-            $this->fireOnce(new Grid\Events\ApplySelector($this, [$active]));
+            $this->fireOnce(new Grid\Events\ApplySelector([$active]));
 
             $values = $active[$key];
             if ($selector['type'] == 'one') {

+ 6 - 2
src/Grid/Events/Event.php

@@ -13,9 +13,13 @@ abstract class Event
 
     public $payload = [];
 
-    public function __construct(Grid $grid, array $payload = [])
+    public function __construct(array $payload = [])
     {
-        $this->grid = $grid;
         $this->payload = $payload;
     }
+
+    public function setGrid(Grid $grid)
+    {
+        $this->grid = $grid;
+    }
 }

+ 12 - 2
src/Grid/Filter.php

@@ -5,6 +5,8 @@ namespace Dcat\Admin\Grid;
 use Dcat\Admin\Admin;
 use Dcat\Admin\Exception\RuntimeException;
 use Dcat\Admin\Grid\Events\ApplyFilter;
+use Dcat\Admin\Grid\Events\Fetched;
+use Dcat\Admin\Grid\Events\Fetching;
 use Dcat\Admin\Grid\Filter\AbstractFilter;
 use Dcat\Admin\Grid\Filter\Between;
 use Dcat\Admin\Grid\Filter\Date;
@@ -462,7 +464,7 @@ class Filter implements Renderable
             if (! empty($conditions)) {
                 $this->expand();
 
-                $this->grid()->fireOnce(new ApplyFilter($this->grid(), [$conditions]));
+                $this->grid()->fireOnce(new ApplyFilter([$conditions]));
 
                 $this->grid()->model()->disableBindTreeQuery();
             }
@@ -628,7 +630,15 @@ class Filter implements Renderable
             $this->getScopeConditions()
         );
 
-        return $this->model->addConditions($conditions)->buildData($toArray);
+        $this->model->addConditions($conditions);
+
+        $this->grid()->fireOnce(new Fetching());
+
+        $data = $this->model->buildData($toArray);
+
+        $this->grid()->fireOnce(new Fetched([&$data]));
+
+        return $data;
     }
 
     /**

+ 0 - 2
src/Grid/Model.php

@@ -360,8 +360,6 @@ class Model
             $this->setData($this->fetch());
         }
 
-        $this->grid->fireOnce(new Grid\Events\Fetched($this->grid(), [$this->data]));
-
         return $toArray ? $this->data->toArray() : $this->data;
     }