Skip to content

Commit 2b4a126

Browse files
Merge pull request #98 from iotronlab/patch-3
Update SelectTree.php added storeResults() method
2 parents cac6368 + bb39c92 commit 2b4a126

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This package adds a dynamic select tree field to your Laravel / Filament applica
77

88
![Select Tree](https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/d944b896-134b-414a-b654-9adecc43ba5e)
99

10-
1110
## Installation
1211

1312
You can install the package via composer:
@@ -150,14 +149,29 @@ Allow soft deleted items to be displayed
150149
->withTrashed()
151150
```
152151

153-
Specify a different key for your model.
152+
Specify a different key for your model.
154153
For example: you have id, code and parent_code. Your model uses id as key, but the parent-child relation is established between code and parent_code
155154

156155
```PHP
157156
->withKey('code')
158157
```
159158

159+
Store fetched models for additional functionality.
160+
161+
```PHP
162+
->storeResults()
163+
```
164+
165+
Now you can access the results in `afterStateUpdated`
166+
167+
```php
168+
->afterStateUpdated(function ($state, SelectTree $component) {
169+
$component->getResults()
170+
}),
171+
```
172+
160173
## Filters
174+
161175
Use the tree in your table filters. Here's an example to show you how.
162176

163177
```bash
@@ -191,6 +205,7 @@ use CodeWithDennis\FilamentSelectTree\SelectTree;
191205
```
192206

193207
## Screenshots
208+
194209
![download.png](./resources/images/example.png)
195210

196211
## Contributing

src/SelectTree.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class SelectTree extends Field implements HasAffixActions
8282

8383
protected Closure|bool|null $withTrashed = false;
8484

85+
protected bool $storeResults = false;
86+
87+
protected Collection|array|null $results = null;
88+
8589
protected function setUp(): void
8690
{
8791
// Load the state from relationships using a callback function.
@@ -164,6 +168,11 @@ private function buildTree(): Collection
164168
// Combine the results from both queries
165169
$combinedResults = $nullParentResults->concat($nonNullParentResults);
166170

171+
// Store results for additional functionality
172+
if ($this->storeResults) {
173+
$this->results = $combinedResults;
174+
}
175+
167176
return $this->buildTreeFromResults($combinedResults);
168177
}
169178

@@ -183,7 +192,7 @@ private function buildTreeFromResults($results, $parent = null): Collection
183192
// Group results by their parent IDs
184193
foreach ($results as $result) {
185194
$parentId = $result->{$this->getParentAttribute()};
186-
if (! isset($resultMap[$parentId])) {
195+
if (!isset($resultMap[$parentId])) {
187196
$resultMap[$parentId] = [];
188197
}
189198
$resultMap[$parentId][] = $result;
@@ -374,11 +383,23 @@ public function enableBranchNode(bool $enableBranchNode = true): static
374383
return $this;
375384
}
376385

386+
public function storeResults(bool $storeResults = true): static
387+
{
388+
$this->storeResults = $storeResults;
389+
390+
return $this;
391+
}
392+
377393
public function getTree(): Collection|array
378394
{
379395
return $this->evaluate($this->buildTree());
380396
}
381397

398+
public function getResults(): Collection|array|null
399+
{
400+
return $this->results;
401+
}
402+
382403
public function getExpandSelected(): bool
383404
{
384405
return $this->evaluate($this->expandSelected);
@@ -499,7 +520,7 @@ public function getCreateOptionAction(): ?Action
499520
return null;
500521
}
501522

502-
if (! $this->hasCreateOptionActionFormSchema()) {
523+
if (!$this->hasCreateOptionActionFormSchema()) {
503524
return null;
504525
}
505526

@@ -510,7 +531,7 @@ public function getCreateOptionAction(): ?Action
510531
));
511532
})
512533
->action(static function (Action $action, array $arguments, SelectTree $component, array $data, ComponentContainer $form) {
513-
if (! $component->getCreateOptionUsing()) {
534+
if (!$component->getCreateOptionUsing()) {
514535
throw new Exception("Select field [{$component->getStatePath()}] must have a [createOptionUsing()] closure set.");
515536
}
516537

@@ -529,7 +550,7 @@ public function getCreateOptionAction(): ?Action
529550
$component->state($state);
530551
$component->callAfterStateUpdated();
531552

532-
if (! ($arguments['another'] ?? false)) {
553+
if (!($arguments['another'] ?? false)) {
533554
return;
534555
}
535556

0 commit comments

Comments
 (0)