Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive tree rendering with Agile Toolkit

I have a following situation. I have a Model A with following properties: id int name varchar(255) parent_id int (references same Model A).

Now, I need to render Tree View using that ModelA. Of course, I could just load all data, sort it properly by parent_id and "render it" using traditional string sticking. e.g.

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));
}

}

now, I would instead like to use atk4 native lister/smlite template parser for the purpose. but, if you try to do that, then you would end up with nasty lister, where in format row, you would anyway try to substitute the specific tag with output from other lister which in fact you would have to destruct to void runtime memory overflows.

any suggestions?

p.s. code above is not tested, just shows concept

thanks!

like image 540
jancha Avatar asked Aug 30 '11 22:08

jancha


1 Answers

Okay, right time had come and proper add-on has been created. To use it, get your add ons and atk4 up-to-dated and follow this article to get to know how.

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

like image 65
jancha Avatar answered Oct 20 '22 01:10

jancha