Skip to content

Commit

Permalink
Merge pull request #98 from iotronlab/patch-3
Browse files Browse the repository at this point in the history
Update SelectTree.php added storeResults() method
  • Loading branch information
CodeWithDennis authored Apr 7, 2024
2 parents cac6368 + bb39c92 commit 2b4a126
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This package adds a dynamic select tree field to your Laravel / Filament applica

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


## Installation

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

Specify a different key for your model.
Specify a different key for your model.
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

```PHP
->withKey('code')
```

Store fetched models for additional functionality.

```PHP
->storeResults()
```

Now you can access the results in `afterStateUpdated`

```php
->afterStateUpdated(function ($state, SelectTree $component) {
$component->getResults()
}),
```

## Filters

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

```bash
Expand Down Expand Up @@ -191,6 +205,7 @@ use CodeWithDennis\FilamentSelectTree\SelectTree;
```

## Screenshots

![download.png](./resources/images/example.png)

## Contributing
Expand Down
29 changes: 25 additions & 4 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class SelectTree extends Field implements HasAffixActions

protected Closure|bool|null $withTrashed = false;

protected bool $storeResults = false;

protected Collection|array|null $results = null;

protected function setUp(): void
{
// Load the state from relationships using a callback function.
Expand Down Expand Up @@ -164,6 +168,11 @@ private function buildTree(): Collection
// Combine the results from both queries
$combinedResults = $nullParentResults->concat($nonNullParentResults);

// Store results for additional functionality
if ($this->storeResults) {
$this->results = $combinedResults;
}

return $this->buildTreeFromResults($combinedResults);
}

Expand All @@ -183,7 +192,7 @@ private function buildTreeFromResults($results, $parent = null): Collection
// Group results by their parent IDs
foreach ($results as $result) {
$parentId = $result->{$this->getParentAttribute()};
if (! isset($resultMap[$parentId])) {
if (!isset($resultMap[$parentId])) {
$resultMap[$parentId] = [];
}
$resultMap[$parentId][] = $result;
Expand Down Expand Up @@ -374,11 +383,23 @@ public function enableBranchNode(bool $enableBranchNode = true): static
return $this;
}

public function storeResults(bool $storeResults = true): static
{
$this->storeResults = $storeResults;

return $this;
}

public function getTree(): Collection|array
{
return $this->evaluate($this->buildTree());
}

public function getResults(): Collection|array|null
{
return $this->results;
}

public function getExpandSelected(): bool
{
return $this->evaluate($this->expandSelected);
Expand Down Expand Up @@ -499,7 +520,7 @@ public function getCreateOptionAction(): ?Action
return null;
}

if (! $this->hasCreateOptionActionFormSchema()) {
if (!$this->hasCreateOptionActionFormSchema()) {
return null;
}

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

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

if (! ($arguments['another'] ?? false)) {
if (!($arguments['another'] ?? false)) {
return;
}

Expand Down

0 comments on commit 2b4a126

Please sign in to comment.