瀏覽代碼

树形表格查询调整

jqh 5 年之前
父節點
當前提交
963f859bac
共有 5 個文件被更改,包括 107 次插入42 次删除
  1. 16 12
      src/Grid/Column/Filter.php
  2. 7 2
      src/Grid/Concerns/HasQuickSearch.php
  3. 38 0
      src/Grid/Concerns/HasTree.php
  4. 1 1
      src/Grid/Displayers/Tree.php
  5. 45 27
      src/Support/Helper.php

+ 16 - 12
src/Grid/Column/Filter.php

@@ -42,7 +42,13 @@ abstract class Filter implements Renderable
     {
         $this->parent = $column;
 
-        $this->addResetButton();
+        $this->parent->grid()->fetching(function () {
+            $this->addResetButton();
+
+            $this->parent->grid()->model()->treeUrlWithoutQuery(
+                $this->queryName()
+            );
+        });
 
         foreach ($this->resolvings as $closure) {
             $closure($this);
@@ -118,18 +124,16 @@ abstract class Filter implements Renderable
      */
     protected function addResetButton()
     {
-        $this->parent->grid()->fetching(function () {
-            $value = $this->value();
-            if ($value === '' || $value === null) {
-                return;
-            }
+        $value = $this->value();
+        if ($value === '' || $value === null) {
+            return;
+        }
 
-            $style = $this->shouldDisplay() ? 'style=\'margin:3px 12px\'' : '';
+        $style = $this->shouldDisplay() ? 'style=\'margin:3px 12px\'' : '';
 
-            return $this->parent->addHeader(
-                "&nbsp;<a class='fa fa-undo' href='{$this->urlWithoutFilter()}' {$style}></a>"
-            );
-        });
+        return $this->parent->addHeader(
+            "&nbsp;<a class='fa fa-undo' href='{$this->urlWithoutFilter()}' {$style}></a>"
+        );
     }
 
     /**
@@ -160,7 +164,7 @@ abstract class Filter implements Renderable
      */
     protected function urlWithoutFilter()
     {
-        $query = app('request')->all();
+        $query = request()->query();
         unset($query[$this->queryName()]);
 
         return Helper::urlWithQuery(url()->current(), $query);

+ 7 - 2
src/Grid/Concerns/HasQuickSearch.php

@@ -93,10 +93,15 @@ trait HasQuickSearch
             return;
         }
 
-        if (! $query = request()->get($this->quickSearch->queryName())) {
+        $query = request()->get($this->quickSearch->queryName());
+
+        if ($query === '' || $query === null) {
             return;
         }
 
+        $this->model()->disableBindTreeQuery();
+        $this->model()->treeUrlWithoutQuery($this->quickSearch->queryName());
+
         if ($this->search instanceof \Closure) {
             return call_user_func($this->search, $this->model(), $query);
         }
@@ -123,7 +128,7 @@ trait HasQuickSearch
     {
         $queries = preg_split('/\s(?=([^"]*"[^"]*")*[^"]*$)/', trim($query));
         if (! $queries = $this->parseQueryBindings($queries)) {
-            $this->addWhereBasicBinding($this->getKeyName(), false, '=', '___');
+            $this->addWhereBasicBinding($this->keyName(), false, '=', '___');
 
             return;
         }

+ 38 - 0
src/Grid/Concerns/HasTree.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Grid\Concerns;
 
 use Dcat\Admin\Admin;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Support\Collection;
 
 trait HasTree
@@ -28,6 +29,13 @@ trait HasTree
     protected $allowedTreeQuery = true;
 
     /**
+     * @var array
+     */
+    protected $treeIgnoreQueryNames = [];
+
+    /**
+     * 开启树形表格功能
+     *
      * @param bool $showAll
      * @param bool $sortable
      *
@@ -63,6 +71,11 @@ trait HasTree
         });
     }
 
+    /**
+     * 禁止树形表格查询
+     *
+     * @return $this
+     */
     public function disableBindTreeQuery()
     {
         $this->allowedTreeQuery = false;
@@ -80,6 +93,31 @@ trait HasTree
         });
     }
 
+    /**
+     * 设置子节点查询链接需要忽略的字段
+     *
+     * @param string|array $keys
+     *
+     * @return $this
+     */
+    public function treeUrlWithoutQuery($keys)
+    {
+        $this->treeIgnoreQueryNames = array_merge(
+            $this->treeIgnoreQueryNames,
+            (array) $keys
+        );
+
+        return $this;
+    }
+
+    public function generateTreeUrl()
+    {
+        return Helper::urlWithoutQuery(
+            $this->grid->filter()->urlWithoutFilters(),
+            $this->treeIgnoreQueryNames
+        );
+    }
+
     protected function buildChildrenNodesPagination()
     {
         if ($this->grid()->allowPagination()) {

+ 1 - 1
src/Grid/Displayers/Tree.php

@@ -8,10 +8,10 @@ class Tree extends AbstractDisplayer
 {
     protected function setupScript()
     {
-        $url = $this->grid->filter()->urlWithoutFilters();
         $tableId = $this->grid->tableId();
 
         $model = $this->grid->model();
+        $url = $model->generateTreeUrl();
 
         // 是否显示下一页按钮
         $pageName = $model->getChildrenPageName(':key');

+ 45 - 27
src/Support/Helper.php

@@ -154,6 +154,51 @@ class Helper
         return $url.'?'.http_build_query(array_merge($originalQuery, $query));
     }
 
+
+    /**
+     * @param string                 $url
+     * @param string|array|Arrayable $keys
+     *
+     * @return string
+     */
+    public static function urlWithoutQuery($url, $keys)
+    {
+        if (! Str::contains($url, '?') || ! $keys) {
+            return $url;
+        }
+
+        if ($keys instanceof Arrayable) {
+            $keys = $keys->toArray();
+        }
+
+        $keys = (array) $keys;
+
+        $urlInfo = parse_url($url);
+
+        parse_str($urlInfo['query'], $query);
+
+        Arr::forget($query, $keys);
+
+        $baseUrl = "{$urlInfo['scheme']}://{$urlInfo['host']}{$urlInfo['path']}";
+
+        return $query
+            ? $baseUrl.'?'.http_build_query($query)
+            : $baseUrl;
+    }
+
+
+    /**
+     * Get full url without query strings.
+     *
+     * @param Arrayable|array|string $keys
+     *
+     * @return string
+     */
+    public static function fullUrlWithoutQuery($keys)
+    {
+        return static::urlWithoutQuery(request()->fullUrl(), $keys);
+    }
+
     /**
      * If a request match the specific path.
      *
@@ -325,31 +370,4 @@ class Helper
             }
         }
     }
-
-    /**
-     * Get full url without query strings.
-     *
-     * @param Arrayable|array|string $keys
-     *
-     * @return string
-     */
-    public static function fullUrlWithoutQuery($keys)
-    {
-        if ($keys instanceof Arrayable) {
-            $keys = $keys->toArray();
-        }
-
-        $keys = (array) $keys;
-
-        $request = request();
-
-        $query = $request->query();
-        Arr::forget($query, $keys);
-
-        $question = $request->getBaseUrl().$request->getPathInfo() == '/' ? '/?' : '?';
-
-        return count($request->query()) > 0
-            ? $request->url().$question.http_build_query($query)
-            : $request->fullUrl();
-    }
 }