Skip to content

Commit 811872f

Browse files
Merge pull request #55 from buzkall/3.x
Allow to hide elements in tree
2 parents f962d13 + f5c649e commit 811872f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ Disable specific options in the tree
169169
->pluck('id')
170170
->toArray();
171171
})
172+
173+
```
174+
Hide specific options in the tree
175+
176+
```PHP
177+
->hiddenOptions([2, 3, 4])
178+
```
179+
180+
```PHP
181+
->hiddenOptions(function () {
182+
return Category::where('is_hidden', true)
183+
->get()
184+
->pluck('id')
185+
->toArray();
186+
})
172187
```
173188

174189

src/SelectTree.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class SelectTree extends Field
5151

5252
protected Closure|array $disabledOptions = [];
5353

54+
protected Closure|array $hiddenOptions = [];
55+
5456
protected function setUp(): void
5557
{
5658
// Load the state from relationships using a callback function.
@@ -129,32 +131,40 @@ private function buildTreeFromResults($results, $parent = null): Collection
129131
// Define disabled options
130132
$disabledOptions = $this->getDisabledOptions();
131133

134+
// Define hidden options
135+
$hiddenOptions = $this->getHiddenOptions();
136+
132137
// Recursively build the tree starting from the root (null parent)
133138
$rootResults = $resultMap[$parent] ?? [];
134139
foreach ($rootResults as $result) {
135140
// Build a node and add it to the tree
136-
$node = $this->buildNode($result, $resultMap, $disabledOptions);
141+
$node = $this->buildNode($result, $resultMap, $disabledOptions, $hiddenOptions);
137142
$tree->push($node);
138143
}
139144

140145
return $tree;
141146
}
142147

143-
private function buildNode($result, $resultMap, $disabledOptions): array
148+
private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions): array
144149
{
145150
// Create a node with 'name' and 'value' attributes
146151
$node = [
147152
'name' => $result->{$this->getTitleAttribute()},
148153
'value' => $result->getKey(),
149154
'disabled' => in_array($result->getKey(), $disabledOptions),
155+
'hidden' => in_array($result->getKey(), $hiddenOptions),
150156
];
151157

152158
// Check if the result has children
153159
if (isset($resultMap[$result->getKey()])) {
154160
$children = collect();
155161
// Recursively build child nodes
156162
foreach ($resultMap[$result->getKey()] as $child) {
157-
$childNode = $this->buildNode($child, $resultMap, $disabledOptions);
163+
// don't add the hidden ones
164+
if (in_array($child->getKey(), $hiddenOptions)) {
165+
continue;
166+
}
167+
$childNode = $this->buildNode($child, $resultMap, $disabledOptions, $hiddenOptions);
158168
$children->push($childNode);
159169
}
160170
// Add children to the node
@@ -264,6 +274,13 @@ public function disabledOptions(Closure|array $disabledOptions): static
264274
return $this;
265275
}
266276

277+
public function hiddenOptions(Closure|array $hiddenOptions): static
278+
{
279+
$this->hiddenOptions = $hiddenOptions;
280+
281+
return $this;
282+
}
283+
267284
public function alwaysOpen(bool $alwaysOpen = true): static
268285
{
269286
$this->alwaysOpen = $alwaysOpen;
@@ -342,4 +359,9 @@ public function getDisabledOptions(): array
342359
{
343360
return $this->evaluate($this->disabledOptions);
344361
}
362+
363+
public function getHiddenOptions(): array
364+
{
365+
return $this->evaluate($this->hiddenOptions);
366+
}
345367
}

0 commit comments

Comments
 (0)