Skip to content

Commit 47e5b27

Browse files
Merge pull request #195 from nathanheffley/add-get-tree-using
Add getTreeUsing
2 parents 83a8eb3 + 7e4e88e commit 47e5b27

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,66 @@ If you need to append an item to the tree menu, use the `append` method. This me
275275
])
276276
```
277277

278+
If you need full control over the tree structure, you can use `getTreeUsing` to provide a custom tree array, or a closure that resolves to an array.
279+
280+
Using an array:
281+
282+
```php
283+
SelectTree::make('categories')
284+
->getTreeUsing([
285+
[
286+
'name' => 'Parent Category',
287+
'value' => '1',
288+
'children' => [
289+
[
290+
'name' => 'Child Category',
291+
'value' => '2',
292+
'children' => [],
293+
],
294+
],
295+
],
296+
[
297+
'name' => 'Another Category',
298+
'value' => '3',
299+
'children' => [],
300+
],
301+
])
302+
```
303+
304+
Using a closure for dynamic data:
305+
306+
```php
307+
SelectTree::make('categories')
308+
->getTreeUsing(function () {
309+
return Category::query()
310+
->get()
311+
->map(fn ($category) => [
312+
'name' => $category->name,
313+
'value' => $category->id,
314+
'children' => $category->children->map(fn ($child) => [
315+
'name' => $child->name,
316+
'value' => $child->id,
317+
'children' => [],
318+
])->toArray(),
319+
])
320+
->toArray();
321+
})
322+
```
323+
324+
The tree structure should follow this format:
325+
326+
```php
327+
[
328+
[
329+
'name' => 'Display Name',
330+
'value' => 'option_value',
331+
'disabled' => false, // optional
332+
'hidden' => false, // optional
333+
'children' => [], // optional
334+
],
335+
]
336+
```
337+
278338
## Filters
279339

280340
Use the tree in your table filters. Here's an example to show you how.

src/SelectTree.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class SelectTree extends Field implements HasAffixActions
9494

9595
protected bool $storeResults = false;
9696

97+
protected Closure|array|null $getTreeUsing = null;
98+
9799
protected LazyCollection|array|null $results = null;
98100

99101
protected Closure|bool|null $multiple = null;
@@ -542,8 +544,19 @@ public function storeResults(bool $storeResults = true): static
542544
return $this;
543545
}
544546

547+
public function getTreeUsing(Closure|array $value): static
548+
{
549+
$this->getTreeUsing = $value;
550+
551+
return $this;
552+
}
553+
545554
public function getTree(): Collection|array
546555
{
556+
if ($this->getTreeUsing) {
557+
return $this->evaluate($this->getTreeUsing);
558+
}
559+
547560
return $this->evaluate($this->buildTree()
548561
->when($this->prepend, fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend)))
549562
->when($this->append, fn (Collection $tree) => $tree->push($this->evaluate($this->append)))

0 commit comments

Comments
 (0)