使用Agile Toolkit进行递归树渲染

jan*_*cha 6 php recursion atk4

我有以下情况.我有一个具有以下属性的模型A:id int name varchar(255)parent_id int(引用相同的模型A).

现在,我需要使用ModelA渲染树视图.当然,我可以加载所有数据,通过parent_id正确排序,并使用传统的字符串粘贴"渲染".例如

class Model_A extends Model_Table {
...

function render_branch($nodes, $parent){
    if (!isset($nodes[$parent])){
        return null;
    }
    $out = "<ul>";
    foreach ($nodes[$parent] as $node){
        $out .= "<li>" . $node["name"];
        $out .= $this->render_branch($nodes, $node["id"]);
        $out .= "</li>";
    }
    return $out;
}

function init(){
    parent::init();
    $nodes = array(); // preload from db and arrange so that key = parent and content is array of childs
    $this->template->set("tree", $this->render_branch($nodes, 0));
}

}
Run Code Online (Sandbox Code Playgroud)

现在,我想改为使用atk4 native lister/smlite模板解析器.但是,如果你试图这样做,那么你最终会遇到讨厌的lister,在格式行中,你无论如何都会尝试用其他lister的输出替换特定标记,实际上你必须破坏运行时内存溢出.

有什么建议?

上面的ps代码没有经过测试,只是显示了概念

谢谢!

jan*_*cha 3

好的,正确的时间已经到来,正确的附加组件已经创建。要使用它,请更新您的附加组件和 atk4,并按照本文了解如何使用。

http://www.ambienttech.lv/blog/2012-07-06/tree_view_in_agile_toolkit.html