Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Update SelectTree.php added storeResults() method to get the data in disabledOptions()
  • Loading branch information
iotron committed Apr 8, 2024
1 parent 08efd24 commit 0b58bc7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ For example: you have id, code and parent_code. Your model uses id as key, but t
->withKey('code')
```

Store fetched models for additional functionality.

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

Now you can access the results in disabledOptions/hiddenOptions for custom functions like making only a branch node family selectable.

```PHP
// Note: $component->getResults() is null in afterStateUpdated() as the tree is rebuilt
->disabledOptions(function ($state, SelectTree $component) {
if ($state) {
$results = $component->getResults();
$selectedNode = $results->firstWhere('id', $state[0]);
$selectedFamily = $results->where('parent_id', $selectedNode?->parent_id)->pluck('id')->toArray();

while ($selectedNode?->parent_id) {
$selectedFamily[] = $selectedNode?->parent_id;
$selectedNode = $results->firstWhere('id', $selectedNode?->parent_id);
}
return $results->whereNotIn('id', $selectedFamily)->pluck('id')->toArray();
}
return [];
})
```

## Filters

Expand Down Expand Up @@ -205,9 +230,9 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [CodeWithDennis](https://github.com/CodeWithDennis)
- [Dipson88](https://github.com/dipson88/treeselectjs)
- [All Contributors](../../contributors)
- [CodeWithDennis](https://github.com/CodeWithDennis)
- [Dipson88](https://github.com/dipson88/treeselectjs)
- [All Contributors](../../contributors)

## License

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 0b58bc7

Please sign in to comment.