Skip to content

Commit 95d499f

Browse files
Merge pull request #70 from buzkall/3.x
Add `withKey()` method
2 parents 8003d74 + 462cd8e commit 95d499f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ Hide specific options in the tree
132132
->hiddenOptions([2, 3, 4])
133133
```
134134

135+
Specify a different key for your model.
136+
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
137+
138+
```PHP
139+
->withKey('code')
140+
```
141+
135142
## Filters
136143
Use the tree in your table filters. Here's an example to show you how.
137144

src/SelectTree.php

+21-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class SelectTree extends Field implements HasAffixActions
3737

3838
protected bool $independent = true;
3939

40+
protected string $customKey = null;
41+
4042
protected string $titleAttribute;
4143

4244
protected string $parentAttribute;
@@ -111,7 +113,7 @@ protected function setUp(): void
111113

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

114-
return $record->getKey();
116+
return $record->{$this->getCustomKey()};
115117
});
116118

117119
$this->suffixActions([
@@ -184,18 +186,18 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
184186
// Create a node with 'name' and 'value' attributes
185187
$node = [
186188
'name' => $result->{$this->getTitleAttribute()},
187-
'value' => $result->getKey(),
188-
'disabled' => in_array($result->getKey(), $disabledOptions),
189-
'hidden' => in_array($result->getKey(), $hiddenOptions),
189+
'value' => $result->{$this->getCustomKey()},
190+
'disabled' => in_array($result->{$this->getCustomKey()}, $disabledOptions),
191+
'hidden' => in_array($result->{$this->getCustomKey()}, $hiddenOptions),
190192
];
191193

192194
// Check if the result has children
193-
if (isset($resultMap[$result->getKey()])) {
195+
if (isset($resultMap[$result->{$this->getCustomKey()}])) {
194196
$children = collect();
195197
// Recursively build child nodes
196-
foreach ($resultMap[$result->getKey()] as $child) {
198+
foreach ($resultMap[$result->{$this->getCustomKey()}] as $child) {
197199
// don't add the hidden ones
198-
if (in_array($child->getKey(), $hiddenOptions)) {
200+
if (in_array($child->{$this->getCustomKey()}, $hiddenOptions)) {
199201
continue;
200202
}
201203
$childNode = $this->buildNode($child, $resultMap, $disabledOptions, $hiddenOptions);
@@ -301,6 +303,13 @@ public function independent(bool $independent = true): static
301303
return $this;
302304
}
303305

306+
public function withKey(string $customKey = 'id'): static
307+
{
308+
$this->customKey = $customKey;
309+
310+
return $this;
311+
}
312+
304313
public function disabledOptions(Closure|array $disabledOptions): static
305314
{
306315
$this->disabledOptions = $disabledOptions;
@@ -348,6 +357,11 @@ public function getIndependent(): bool
348357
{
349358
return $this->evaluate($this->independent);
350359
}
360+
361+
public function getCustomKey(): string
362+
{
363+
return $this->customKey ? $this->evaluate($this->customKey) : $this->getKey();
364+
}
351365

352366
public function getWithCount(): bool
353367
{

0 commit comments

Comments
 (0)