contributte / datagrid

:muscle: DataGrid for Nette Framework: filtering, sorting, pagination, tree view, table view, translator, etc

Home Page:https://contributte.org/packages/contributte/datagrid/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Treeview - child nodes

MikKuba opened this issue · comments

I have problem with child nodes in treeview.

This is how I set dataSource - result is array, because example does not work good with Doctrine. IMO array is OK.

$grid->setDataSource($this->getResourcesTree($this->facade->getResources([], "priority")));
$grid->setTreeView([$this, 'getChildren'], 'has_children');

And functions for creating tree:

    private function getResourcesTree(array $resources, $parentId = null, $fromParent = null): array
    {
        $return = [];
        foreach ($resources as $resource) {
            if ($resource['parentId'] == $parentId) {
                $children = $this->getResourcesTree($resources, $resource['id']);
                if($children) {
                    $resource['children'] = $children;
                }
                $resource['has_children'] = isset($resource['children']) ? count($resource['children']) : 0;
                
                $return[$resource['id']] = $resource;
            }
        }
        if($fromParent) {
            $return = $this->getChildrenTree($return, (int)$fromParent);
        }
        
        return $return;
    }

    private function getChildrenTree(array $resources, int $fromParent, $firstLoop = true): array
    {
        foreach($resources as $value){
            if($value['id'] == $fromParent) {
                return $value['children'];
            }
            if(!$firstLoop && $value['parentId'] == $fromParent) {
                if(isset($value['children'])) {
                    return $value['children'];
                }
                else{
                    return $value;
                }
            }
            if(($value['has_children'] > 0) && !empty($value['children'])) {
                $children = $this->getChildrenTree($value['children'], $fromParent, false);
                if($children) {
                    return $children;
                }
            }
        }
        
        
        return [];
    }

And callback for nodes with children:

    public function getChildren($parentId): array
    {
        return $this->getResourcesTree($this->facade->getResources([], "priority"), null, $parentId);
    }

Now the problem, what's wrong.
When I open first level node (meaning from root to first level), every child nodes appear correctly. But, if this node has another children and I try to show them, page reload and all nodes are packed up.
And also - when I successfully opened first level from root, same issue is when i want to click edit on this child node.

Do you have any idea, what's wrong with my code, or is it some issue of this library?
Thanks a lot for any suggestions!