소스 검색

增加模型树以及树形表格自定义默认parent_id值功能

jqh 4 년 전
부모
커밋
4bf064127d
3개의 변경된 파일43개의 추가작업 그리고 5개의 파일을 삭제
  1. 3 2
      src/Grid/Column/HasDisplayers.php
  2. 30 2
      src/Grid/Concerns/HasTree.php
  3. 10 1
      src/Traits/ModelTree.php

+ 3 - 2
src/Grid/Column/HasDisplayers.php

@@ -224,12 +224,13 @@ trait HasDisplayers
      *
      * @param bool $showAll
      * @param bool $sortable
+     * @param mixed $defaultParentId
      *
      * @return $this
      */
-    public function tree(bool $showAll = false, bool $sortable = true)
+    public function tree(bool $showAll = false, bool $sortable = true, $defaultParentId = null)
     {
-        $this->grid->model()->enableTree($showAll, $sortable);
+        $this->grid->model()->enableTree($showAll, $sortable, $defaultParentId);
 
         $this->grid->listen(Grid\Events\Fetching::class, function () use ($showAll) {
             if ($this->grid->model()->getParentIdFromRequest()) {

+ 30 - 2
src/Grid/Concerns/HasTree.php

@@ -5,6 +5,7 @@ namespace Dcat\Admin\Grid\Concerns;
 use Dcat\Admin\Admin;
 use Dcat\Admin\Grid\Events\Fetched;
 use Dcat\Admin\Grid\Events\Fetching;
+use Dcat\Admin\Repositories\EloquentRepository;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Support\Collection;
 
@@ -35,17 +36,24 @@ trait HasTree
      */
     protected $treeIgnoreQueryNames = [];
 
+    /**
+     * @var mixed
+     */
+    protected $defaultParentId;
+
     /**
      * 开启树形表格功能.
      *
      * @param bool $showAll
      * @param bool $sortable
+     * @param mixed $defaultParentId
      *
      * @return void
      */
-    public function enableTree(bool $showAll, bool $sortable)
+    public function enableTree(bool $showAll, bool $sortable, $defaultParentId = null)
     {
         $this->showAllChildrenNodes = $showAll;
+        $this->defaultParentId = $defaultParentId;
 
         $this->grid()->listen(Fetching::class, function () use ($sortable) {
             $this->sortTree($sortable);
@@ -213,7 +221,27 @@ HTML
     {
         return $this->request->get(
             $this->getParentIdQueryName()
-        ) ?: 0;
+        ) ?: $this->getDefaultParentId();
+    }
+
+    /**
+     * 获取默认parent_id字段值.
+     *
+     * @return int|mixed
+     */
+    public function getDefaultParentId()
+    {
+        if ($this->defaultParentId !== null) {
+            return $this->defaultParentId;
+        }
+
+        $repository = $this->grid->model()->repository();
+
+        if ($repository instanceof EloquentRepository) {
+            return $repository->model()->getDefaultParentId();
+        }
+
+        return 0;
     }
 
     /**

+ 10 - 1
src/Traits/ModelTree.php

@@ -15,6 +15,7 @@ use Spatie\EloquentSortable\SortableTrait;
  * @property string $parentColumn
  * @property string $titleColumn
  * @property string $orderColumn
+ * @property string $defaultParentId
  * @property array  $sortable
  */
 trait ModelTree
@@ -59,6 +60,14 @@ trait ModelTree
         return empty($this->orderColumn) ? 'order' : $this->orderColumn;
     }
 
+    /**
+     * @return string
+     */
+    public function getDefaultParentId()
+    {
+        return empty($this->defaultParentId) ? '0' : $this->defaultParentId;
+    }
+
     /**
      * Set query callback to model.
      *
@@ -86,7 +95,7 @@ trait ModelTree
 
         return Helper::buildNestedArray(
             $nodes,
-            0,
+            $this->getDefaultParentId(),
             $this->getKeyName(),
             $this->getParentColumn()
         );