diff --git a/README.md b/README.md index 8902756..d879719 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,29 @@ Specify the list's force direction. Options include: auto (default), top, and bo ->direction('top') ``` +Customize how the tree is built with a custom callback function + +```php +->getTreeUsing(function () { + // Build and return your custom tree structure as a Collection + return collect([ + [ + 'name' => 'Custom Category 1', + 'value' => 1, + 'parent' => null, + 'children' => [ + [ + 'name' => 'Subcategory 1.1', + 'value' => 2, + 'parent' => 1, + 'children' => [] + ] + ] + ] + ]); +}) +``` + Display individual leaf nodes instead of the main group when all leaf nodes are selected ```php diff --git a/src/SelectTree.php b/src/SelectTree.php index e809057..c4ce70a 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -88,6 +88,8 @@ class SelectTree extends Field implements HasAffixActions protected Closure|array|null $prepend = null; + protected ?Closure $getTreeUsing = null; + protected function setUp(): void { // Load the state from relationships using a callback function. @@ -149,6 +151,10 @@ protected function setUp(): void protected function buildTree(): Collection { + if ($this->getTreeUsing) { + return $this->evaluate($this->getTreeUsing); + } + // Start with two separate query builders $nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue()); $nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), $this->getParentNullValue()); @@ -418,7 +424,7 @@ public function getTree(): Collection|array public function getResults(): Collection|array|null { - return $this->evaluate($this->results); + return $this->results; } public function getExpandSelected(): bool @@ -614,4 +620,11 @@ public function createOptionModalHeading(string|Closure|null $heading): static return $this; } + + public function getTreeUsing(?Closure $callback): static + { + $this->getTreeUsing = $callback; + + return $this; + } }