-
Notifications
You must be signed in to change notification settings - Fork 113
Add Model support for TreeItemSelector control source data #2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 7 commits
cb6fb64
e160b21
d0742d6
b42a49c
4fe23b7
06a821e
416abe6
09bc291
fb7a1a8
17306b9
faf1f8a
590bbc6
63d04a6
7024eb2
d54c7ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace Atk4\Ui\Form\Control; | ||
|
|
||
| use Atk4\Data\Model; | ||
| use Atk4\Ui\Form; | ||
| use Atk4\Ui\HtmlTemplate; | ||
| use Atk4\Ui\Js\Jquery; | ||
|
|
@@ -39,6 +40,13 @@ class TreeItemSelector extends Form\Control | |
| */ | ||
| public $loaderCssName = 'atk-tree-loader'; | ||
|
|
||
| /** | ||
| * The field name which includes the parent node's id. | ||
mkrecek234 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @var string | ||
| */ | ||
| public $parentIdField = 'parent_id'; | ||
|
|
||
| /** @var bool Allow multiple selection or just one. */ | ||
| public $allowMultiple = true; | ||
|
|
||
|
|
@@ -112,6 +120,46 @@ public function setTreeItems(array $treeItems) | |
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Recursive function to return sub-node for a given parent. | ||
| * | ||
| * @param $model : Model | ||
mkrecek234 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param $nodes: array | ||
| * @param $parentId : int|string|null | ||
| * | ||
| * @return array|null | ||
| */ | ||
| protected function addNodes(Model $model, array $nodes, $parentId = null) | ||
mkrecek234 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // return an array of items with parent = $parentId | ||
| $result = []; | ||
| foreach ($nodes as $node) { | ||
| if ($node[$this->parentIdField] === $parentId) { | ||
| $newNode = []; | ||
| $newNode['name'] = $node[$model->titleField]; | ||
| $newNode['id'] = $node[$model->idField]; | ||
| $newNode['parent_id'] = $node[$this->parentIdField]; | ||
| $newNode['nodes'] = $this->addNodes($model, $nodes, $newNode['id']); | ||
| $result[] = $newNode; | ||
| } | ||
| } | ||
|
|
||
| if (count($result) > 0) { | ||
mkrecek234 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return $result; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public function setModel(Model $model): void | ||
| { | ||
| parent::setModel($model); | ||
|
|
||
| $nodes_array = (clone $this->model)->export(); | ||
|
||
| $this->treeItems = $this->addNodes($model, $nodes_array); | ||
| unset($nodes_array); | ||
mkrecek234 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Returns <input ...> tag. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.