Skip to content

Commit

Permalink
Merge pull request #75 from buzkall/with-key
Browse files Browse the repository at this point in the history
Fixed add withKey method
  • Loading branch information
CodeWithDennis authored Feb 6, 2024
2 parents 0e3a2f0 + 47b7faa commit db8ca2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ Hide specific options in the tree
->hiddenOptions([2, 3, 4])
```

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')
```

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

Expand Down
30 changes: 23 additions & 7 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class SelectTree extends Field implements HasAffixActions

protected bool $independent = true;

protected ?string $customKey = null;

protected string $titleAttribute;

protected string $parentAttribute;
Expand Down Expand Up @@ -111,7 +113,7 @@ protected function setUp(): void

$form->model($record)->saveRelationships();

return $record->getKey();
return $this->getCustomKey($record);
});

$this->suffixActions([
Expand Down Expand Up @@ -181,22 +183,24 @@ private function buildTreeFromResults($results, $parent = null): Collection

private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions): array
{
$key = $this->getCustomKey($result);

// Create a node with 'name' and 'value' attributes
$node = [
'name' => $result->{$this->getTitleAttribute()},
'value' => $result->getKey(),
'value' => $key,
'parent' => $result->{$this->getParentAttribute()},
'disabled' => in_array($result->getKey(), $disabledOptions),
'hidden' => in_array($result->getKey(), $hiddenOptions),
'disabled' => in_array($key, $disabledOptions),
'hidden' => in_array($key, $hiddenOptions),
];

// Check if the result has children
if (isset($resultMap[$result->getKey()])) {
if (isset($resultMap[$key])) {
$children = collect();
// Recursively build child nodes
foreach ($resultMap[$result->getKey()] as $child) {
foreach ($resultMap[$key] as $child) {
// don't add the hidden ones
if (in_array($child->getKey(), $hiddenOptions)) {
if (in_array($this->getCustomKey($child), $hiddenOptions)) {
continue;
}
$childNode = $this->buildNode($child, $resultMap, $disabledOptions, $hiddenOptions);
Expand Down Expand Up @@ -302,6 +306,13 @@ public function independent(bool $independent = true): static
return $this;
}

public function withKey(string $customKey): static
{
$this->customKey = $customKey;

return $this;
}

public function disabledOptions(Closure|array $disabledOptions): static
{
$this->disabledOptions = $disabledOptions;
Expand Down Expand Up @@ -350,6 +361,11 @@ public function getIndependent(): bool
return $this->evaluate($this->independent);
}

public function getCustomKey($record)
{
return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey};
}

public function getWithCount(): bool
{
return $this->evaluate($this->withCount);
Expand Down

0 comments on commit db8ca2e

Please sign in to comment.