Pārlūkot izejas kodu

导出功能代码调整

jqh 5 gadi atpakaļ
vecāks
revīzija
db4b6e5520

+ 1 - 19
src/Grid.php

@@ -264,7 +264,7 @@ class Grid
      */
     public function number(?string $label = null)
     {
-        return $this->column('#', $label ?: '#')->bold();
+        return $this->addColumn('#', $label ?: '#')->bold();
     }
 
     /**
@@ -323,24 +323,6 @@ class Grid
         return $column;
     }
 
-    /**
-     * Prepend column to grid.
-     *
-     * @param string $column
-     * @param string $label
-     *
-     * @return Column
-     */
-    protected function prependColumn($column = '', $label = '')
-    {
-        $column = new Column($column, $label);
-        $column->setGrid($this);
-
-        $this->columns->prepend($column);
-
-        return $column;
-    }
-
     /**
      * Get Grid model.
      *

+ 12 - 4
src/Grid/Concerns/HasElementNames.php

@@ -2,6 +2,12 @@
 
 namespace Dcat\Admin\Grid\Concerns;
 
+use Dcat\Admin\Grid;
+
+/**
+ * @method Grid\Model model()
+ * @method Grid\Filter getFilter()
+ */
 trait HasElementNames
 {
     /**
@@ -36,14 +42,16 @@ trait HasElementNames
     {
         $this->__name = $name;
 
-        $m = $this->model();
+        $model = $this->model();
 
-        $m->setPerPageName("{$name}_{$m->getPerPageName()}")
-            ->setPageName("{$name}_{$m->getPageName()}")
-            ->setSortName("{$name}_{$m->getSortName()}");
+        $model->setPerPageName("{$name}_{$model->getPerPageName()}")
+            ->setPageName("{$name}_{$model->getPageName()}")
+            ->setSortName("{$name}_{$model->getSortName()}");
 
         $this->getFilter()->setName($name);
 
+        $this->setExporterQueryName($name);
+
         return $this;
     }
 

+ 41 - 10
src/Grid/Concerns/HasExporter.php

@@ -10,12 +10,17 @@ use Illuminate\Support\Facades\Input;
 
 trait HasExporter
 {
+    /**
+     * @var Exporter
+     */
+    protected $exporter;
+
     /**
      * Export driver.
      *
      * @var string|Grid\Exporters\AbstractExporter
      */
-    protected $exporter;
+    protected $exportDriver;
 
     /**
      * Set exporter driver for Grid to export.
@@ -26,7 +31,7 @@ trait HasExporter
      */
     public function exporter($exporter)
     {
-        $this->exporter = $exporter;
+        $this->exportDriver = $exporter;
 
         return $this;
     }
@@ -35,10 +40,15 @@ trait HasExporter
      * Handle export request.
      *
      * @param bool $forceExport
+     *
+     * @return mixed
      */
     protected function handleExportRequest($forceExport = false)
     {
-        if (!$scope = request(Exporter::$queryName)) {
+        if (
+            ! $this->allowExportBtn()
+            || ! $scope = request($this->getExporter()->getQueryName())
+        ) {
             return;
         }
 
@@ -52,22 +62,42 @@ trait HasExporter
         if ($this->builder) {
             call_user_func($this->builder, $this);
 
-            $this->getExporter($scope)->export();
+            return $this->resolveExportDriver($scope)->export();
         }
 
         if ($forceExport) {
-            $this->getExporter($scope)->export();
+            return $this->resolveExportDriver($scope)->export();
         }
     }
 
+    /**
+     * @return Exporter
+     */
+    protected function getExporter()
+    {
+        return $this->exporter ?: ($this->exporter = new Exporter($this));
+    }
+
+    /**
+     * @param string $gridName
+     */
+    protected function setExporterQueryName($gridName)
+    {
+        if (! $this->allowExportBtn()) {
+            return;
+        }
+
+        $this->getExporter()->setQueryName($gridName.'_export_');
+    }
+
     /**
      * @param string $scope
      *
      * @return AbstractExporter
      */
-    protected function getExporter($scope)
+    protected function resolveExportDriver($scope)
     {
-        return (new Exporter($this))->resolve($this->exporter)->withScope($scope);
+        return $this->getExporter()->resolve($this->exportDriver)->withScope($scope);
     }
 
 
@@ -81,7 +111,7 @@ trait HasExporter
      */
     public function getExportUrl($scope = 1, $args = null)
     {
-        $input = array_merge(Input::all(), Exporter::formatExportQuery($scope, $args));
+        $input = array_merge(Input::all(), $this->getExporter()->formatExportQuery($scope, $args));
 
         if ($constraints = $this->model()->getConstraints()) {
             $input = array_merge($input, $constraints);
@@ -122,9 +152,10 @@ trait HasExporter
      */
     public function renderExportButton()
     {
-        if (!$this->options['show_exporter']) {
+        if (! $this->allowExportBtn()) {
             return '';
         }
+        
         return (new Tools\ExportButton($this))->render();
     }
 
@@ -135,7 +166,7 @@ trait HasExporter
      */
     public function disableExporter(bool $disable = true)
     {
-        return $this->option('show_exporter', !$disable);
+        return $this->option('show_exporter', ! $disable);
     }
 
     /**

+ 24 - 12
src/Grid/Exporter.php

@@ -14,11 +14,6 @@ class Exporter
     const SCOPE_CURRENT_PAGE = 'page';
     const SCOPE_SELECTED_ROWS = 'selected';
 
-    /**
-     * @var Grid
-     */
-    protected $grid;
-
     /**
      * Available exporter drivers.
      *
@@ -31,7 +26,12 @@ class Exporter
      *
      * @var string
      */
-    public static $queryName = '_export_';
+    protected $queryName = '_export_';
+
+    /**
+     * @var Grid
+     */
+    protected $grid;
 
     /**
      * Create a new Exporter instance.
@@ -41,18 +41,30 @@ class Exporter
     public function __construct(Grid $grid)
     {
         $this->grid = $grid;
-
-        $this->grid->model()->usePaginate(false);
     }
 
     /**
      * Set export query name.
      *
      * @param $name
+     *
+     * @return $this
+     */
+    public function setQueryName($name)
+    {
+        $this->queryName = $name;
+
+        return $this;
+    }
+
+    /**
+     * Get export query name.
+     *
+     * @return string
      */
-    public static function setQueryName($name)
+    public function getQueryName(): string
     {
-        static::$queryName = $name;
+        return $this->queryName;
     }
 
     /**
@@ -116,7 +128,7 @@ class Exporter
      *
      * @return array
      */
-    public static function formatExportQuery($scope = '', $args = null)
+    public function formatExportQuery($scope = '', $args = null)
     {
         $query = '';
 
@@ -132,6 +144,6 @@ class Exporter
             $query = "selected:$args";
         }
 
-        return [static::$queryName => $query];
+        return [$this->queryName => $query];
     }
 }

+ 2 - 0
src/Grid/Exporters/AbstractExporter.php

@@ -92,6 +92,8 @@ abstract class AbstractExporter implements ExporterInterface
     {
         $model = $this->grid->model();
 
+        $model->usePaginate(false);
+
         if ($scope == Grid\Exporter::SCOPE_ALL) {
             $model->usePaginate(true);
             $model->setPerPage($this->grid->option('export_limit'));