使用 Agile Toolkit 进行递归树渲染
我有以下情况。我有一个模型 A,具有以下属性: id 整数 名称 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));
}
}
,现在,我想使用 atk4 本机 lister/smlite 模板解析器来实现此目的。但是,如果您尝试这样做,那么您最终会得到令人讨厌的列表器,在格式行中,您无论如何都会尝试用其他列表器的输出替换特定标记,实际上您必须破坏以消除运行时内存溢出。
有什么建议吗?
附注 上面的代码未经测试,仅显示概念
,谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,正确的时间已经到来,正确的附加组件已经创建。要使用它,请更新您的附加组件和 atk4,并按照本文了解如何使用。
http://www.ambienttech.lv/blog/2012-07-06 /tree_view_in_agile_toolkit.html
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
根据 Jancha 的评论
。
As per Jancha's comment
.