public function prepareNestedData($data, $lft = 0, $rgt = null, $root = 0)
    {
        $res = [];
        foreach ($data as $row) {
            $currentRoot = isset($row[$this->rootAttribute]) ? $row[$this->rootAttribute] : 0;
            if (is_null($rgt) || $row[$this->rightAttribute] < $rgt && $root == $currentRoot) {
                if ($lft + 1 == $row[$this->leftAttribute]) {
                    if ($row[$this->leftAttribute] + 1 !== $row[$this->rightAttribute]) {
                        $res[] = [
                            'id' => $row['id'],
                            'text' => $row[$this->modelLabelAttribute],
                            'a_attr' => [
                                'data-id' => $row['id'],
                            ],
                            'children' => self::prepareNestedData(
                                $data,
                                $row[$this->leftAttribute],
                                $row[$this->rightAttribute],
                                $currentRoot
                            ),
                            'state' => [
                                'selected' => in_array($row['id'], $this->selectedNodes),
                            ],
                        ];
                    } else {
                        $res[] = [
                            'id' => $row['id'],
                            'text' => $row[$this->modelLabelAttribute],
                            'a_attr' => [
                                'data-id' => $row['id'],
                            ],
                            'children' => [],
                            'state' => [
                                'selected' => in_array($row['id'], $this->selectedNodes),
                            ],
                        ];
                    }
                    $lft = $row[$this->rightAttribute];
                } else if ($row[$this->leftAttribute] == 1 && $root !== $currentRoot) {
                    $res[] = [
                        'id' => $row['id'],
                        'text' => $row[$this->modelLabelAttribute],
                        'a_attr' => [
                            'data-id' => $row['id'],
                        ],
                        'children' => self::prepareNestedData(
                            $data,
                            $row[$this->leftAttribute],
                            $row[$this->rightAttribute],
                            $currentRoot
                        ),
                        'state' => [
                            'selected' => in_array($row['id'], $this->selectedNodes),
                        ],
                    ];
                }
            }
        }
        return $res;
    }