jqh il y a 5 ans
Parent
commit
c3cb3d04b8

+ 1 - 1
src/Controllers/ExtensionController.php

@@ -115,7 +115,7 @@ class ExtensionController extends Controller
             ->display($view)
             ->expand($this->getExpandHandler('config'))
             ->else()
-            ->asEmpty();
+            ->emptyString();
 
         $grid->require
             ->display($view)

+ 3 - 3
src/Controllers/LogController.php

@@ -45,13 +45,13 @@ class LogController extends Controller
             $color = Arr::get(OperationLogModel::$methodColors, $method, 'default');
 
             return "<span class=\"label label-$color\">$method</span>";
-        })->onlyValueFilter();
+        })->filterByValue();
 
         $grid->path(trans('admin.uri'))->responsive()->display(function ($v) {
             return "<code>$v</code>";
-        })->onlyValueFilter();
+        })->filterByValue();
 
-        $grid->ip('IP')->onlyValueFilter()->responsive();
+        $grid->ip('IP')->filterByValue()->responsive();
 
         $grid->input->responsive()->display(function ($input) {
             $input = json_decode($input, true);

+ 1 - 1
src/Controllers/UserController.php

@@ -111,7 +111,7 @@ class UserController extends Controller
                     }
                 })
                 ->else()
-                ->asEmpty();
+                ->emptyString();
 
             $grid->created_at;
             $grid->updated_at->sortable();

+ 5 - 5
src/Grid/Column.php

@@ -261,7 +261,7 @@ class Column
      *         ->display($view)
      *         ->expand(...)
      *         ->else()
-     *         ->asEmpty()
+     *         ->emptyString()
      *
      *    $grid->config
      *         ->if(function () {
@@ -271,7 +271,7 @@ class Column
      *             $column ->display($view)->expand(...);
      *         })
      *         ->else(function (Column $column) {
-     *             $column->asEmpty();
+     *             $column->emptyString();
      *         })
      *
      * @param \Closure $condition
@@ -497,7 +497,7 @@ class Column
         $valueKey = is_string($filter) || $filter instanceof \Closure ? $filter : null;
 
         if (! $filter || $valueKey) {
-            $filter = Grid\Column\Filter\Equal::make()->valueAsFilter($valueKey);
+            $filter = Grid\Column\Filter\Equal::make()->valueFilter($valueKey);
         }
 
         if (! $filter instanceof Grid\Column\Filter) {
@@ -512,11 +512,11 @@ class Column
      *
      * @return Column
      */
-    public function onlyValueFilter($valueKey = null)
+    public function filterByValue($valueKey = null)
     {
         return $this->filter(
             Grid\Column\Filter\Equal::make()
-                ->valueAsFilter($valueKey)
+                ->valueFilter($valueKey)
                 ->hide()
         );
     }

+ 1 - 1
src/Grid/Column/Filter/Input.php

@@ -74,7 +74,7 @@ HTML;
      *
      * @return $this
      */
-    public function valueAsFilter($valueKey = null)
+    public function valueFilter($valueKey = null)
     {
         return $this->resolving(function () use ($valueKey) {
             $valueFilter = new ValueFilter($this, $valueKey);

+ 1 - 1
src/Grid/Column/HasDisplayers.php

@@ -222,7 +222,7 @@ trait HasDisplayers
     /**
      * @return $this
      */
-    public function asEmpty()
+    public function emptyString()
     {
         return $this->display('');
     }

+ 63 - 22
src/Grid/Concerns/HasTree.php

@@ -22,6 +22,11 @@ trait HasTree
      */
     protected $showAllChildrenNodes = false;
 
+    /**
+     * @var bool
+     */
+    protected $allowedTreeQuery = true;
+
     /**
      * @param bool $showAll
      * @param bool $sortable
@@ -33,22 +38,14 @@ trait HasTree
         $this->showAllChildrenNodes = $showAll;
 
         $this->grid->fetching(function () use ($sortable) {
-            $parentId = $this->getParentIdFromRequest();
+            $this->sortTree($sortable);
+            $this->bindChildrenNodesQuery();
 
-            if (
-                $sortable
-                && ! $this->findQueryByMethod('orderBy')
-                && ! $this->findQueryByMethod('orderByDesc')
-                && ($orderColumn = $this->repository->getOrderColumn())
-            ) {
-                $this->orderBy($orderColumn);
+            if (! $this->getParentIdFromRequest()) {
+                $this->setPageName(
+                    $this->getChildrenPageName($this->getParentIdFromRequest())
+                );
             }
-
-            $this->where($this->repository->getParentColumn(), $parentId);
-
-            $this->setPageName(
-                $this->getChildrenPageName($parentId)
-            );
         });
 
         $this->collection(function (Collection $collection) {
@@ -60,18 +57,62 @@ trait HasTree
                 abort(404);
             }
 
-            if ($this->grid()->allowPagination()) {
-                $nextPage = $this->getCurrentChildrenPage() + 1;
-                Admin::html(
-                    <<<HTML
+            $this->buildChildrenNodesPagination();
+
+            return $collection;
+        });
+    }
+
+    public function disableBindTreeQuery()
+    {
+        $this->allowedTreeQuery = false;
+
+        return $this->filterQueryBy(function ($query) {
+            if (
+                $query['method'] === 'where'
+                && $query['arguments']
+                && $query['arguments'][0] === $this->repository->getParentColumn()
+            ) {
+                return false;
+            }
+
+            return true;
+        });
+    }
+
+    protected function buildChildrenNodesPagination()
+    {
+        if ($this->grid()->allowPagination()) {
+            $nextPage = $this->getCurrentChildrenPage() + 1;
+
+            Admin::html(
+                <<<HTML
 <next-page class="hidden">{$nextPage}</next-page>
 <last-page class="hidden">{$this->paginator()->lastPage()}</last-page>
 HTML
-                );
-            }
+            );
+        }
+    }
 
-            return $collection;
-        });
+    protected function sortTree(bool $sortable)
+    {
+        if (
+            $sortable
+            && ! $this->findQueryByMethod('orderBy')
+            && ! $this->findQueryByMethod('orderByDesc')
+            && ($orderColumn = $this->repository->getOrderColumn())
+        ) {
+            $this->orderBy($orderColumn);
+        }
+    }
+
+    protected function bindChildrenNodesQuery()
+    {
+        if (! $this->allowedTreeQuery) {
+            return;
+        }
+
+        $this->where($this->repository->getParentColumn(), $this->getParentIdFromRequest());
     }
 
     /**

+ 32 - 33
src/Grid/Displayers/Tree.php

@@ -6,41 +6,9 @@ use Dcat\Admin\Admin;
 
 class Tree extends AbstractDisplayer
 {
-    public function display()
-    {
-        $this->setupScript();
-
-        $key = $this->key();
-        $tableId = $this->grid->tableId();
-
-        $level = $this->grid->model()->getLevelFromRequest();
-        $indents = str_repeat(' &nbsp; &nbsp; &nbsp; &nbsp; ', $level);
-
-        return <<<EOT
-<a href="javascript:void(0)" class="{$tableId}-grid-load-children" data-level="{$level}" data-inserted="0" data-key="{$key}">
-   {$indents}<i class="fa fa-angle-right"></i> &nbsp; {$this->value}
-</a>
-EOT;
-    }
-
-    protected function showNextPage()
-    {
-        $model = $this->grid->model();
-
-        $showNextPage = $this->grid->allowPagination();
-        if (! $model->showAllChildrenNodes() && $showNextPage) {
-            $showNextPage =
-                $model->getCurrentChildrenPage() < $model->paginator()->lastPage()
-                && $model->buildData()->count() == $model->getPerPage();
-        }
-
-        return $showNextPage;
-    }
-
     protected function setupScript()
     {
-        // 分页问题
-        $url = request()->fullUrl();
+        $url = $this->grid->filter()->urlWithoutFilters();
         $tableId = $this->grid->tableId();
 
         $model = $this->grid->model();
@@ -156,4 +124,35 @@ EOT;
 JS;
         Admin::script($script);
     }
+
+    public function display()
+    {
+        $this->setupScript();
+
+        $key = $this->key();
+        $tableId = $this->grid->tableId();
+
+        $level = $this->grid->model()->getLevelFromRequest();
+        $indents = str_repeat(' &nbsp; &nbsp; &nbsp; &nbsp; ', $level);
+
+        return <<<EOT
+<a href="javascript:void(0)" class="{$tableId}-grid-load-children" data-level="{$level}" data-inserted="0" data-key="{$key}">
+   {$indents}<i class="fa fa-angle-right"></i> &nbsp; {$this->value}
+</a>
+EOT;
+    }
+
+    protected function showNextPage()
+    {
+        $model = $this->grid->model();
+
+        $showNextPage = $this->grid->allowPagination();
+        if (! $model->showAllChildrenNodes() && $showNextPage) {
+            $showNextPage =
+                $model->getCurrentChildrenPage() < $model->paginator()->lastPage()
+                && $model->buildData()->count() == $model->getPerPage();
+        }
+
+        return $showNextPage;
+    }
 }

+ 5 - 7
src/Grid/Filter.php

@@ -466,6 +466,8 @@ class Filter implements Renderable
         return tap(array_filter($conditions), function ($conditions) {
             if (! empty($conditions)) {
                 $this->expand();
+
+                $this->grid()->model()->disableBindTreeQuery();
             }
         });
     }
@@ -698,13 +700,9 @@ class Filter implements Renderable
         /** @var Collection $columns */
         $columns = $filters->map->column()->flatten();
 
-        $pageKey = 'page';
-
-        if ($gridName = $this->model->grid()->getName()) {
-            $pageKey = "{$gridName}_{$pageKey}";
-        }
-
-        $columns->push($pageKey);
+        $columns->push(
+            $this->grid()->model()->getPageName()
+        );
 
         $groupNames = $filters->filter(function ($filter) {
             return $filter instanceof Group;

+ 25 - 0
src/Grid/Model.php

@@ -613,6 +613,31 @@ class Model
         });
     }
 
+    /**
+     * @param string|callable $method
+     *
+     * @return $this
+     */
+    public function filterQueryBy($method)
+    {
+        $this->queries = $this->queries->filter(function ($query, $k) use ($method) {
+            if (
+                (is_string($method) && $query['method'] === $method)
+                || (is_array($method) && in_array($query['method'], $method, true))
+            ) {
+                return false;
+            }
+
+            if (is_callable($method)) {
+                return call_user_func($method, $query, $k);
+            }
+
+            return true;
+        });
+
+        return $this;
+    }
+
     /**
      * Get the grid sort.
      *