|
@@ -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;
|
|
|
}
|
|
|
|
|
|
/**
|