我正在构建一个图库应用程序,我需要它以树格式,我使用jstree扩展来构建它,我需要json采用这种格式:
$(function () {
$("#demo1").jstree({
"json_data" : {
"data" : [
{
"data" : "A node",
"metadata" : { id : 23 },
"children" : [ "Child 1", "A Child 2" ]
},
{
"attr" : { "id" : "li.node.id1" },
"data" : {
"title" : "Long format demo",
"attr" : { "href" : "#" }
}
}
]
},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) { alert(data.rslt.obj.data("id")); });
});
我正在使用这个数据库:
的categorys: ID 名称 id_father
产品: ID 名称 价钱 CATEGORY_ID
请帮助我们,> _< ,即时通讯在我的项目中使用MVC结构,并且可以执行foreach(modelinstance as model)......
帮助我真的需要它,我不知道如何构建它
答案 0 :(得分:2)
这是我使用的功能,你的模型中需要CNestedSetBehavior,我建议采用这种方式设置分层数据,因为检索起来要快得多:
protected function formatJstree(){
$categories = $this->descendants()->findAll();
$level=0;
$parent = 0;
$data = array();
foreach( $categories as $n => $category )
{
$node = array(
'data'=> "{$category->title}",
'attr'=>array('id'=>"category_id_{$category->category_id}")
);
if($category->level == $level){
$data[$parent]["children"][] = $node;
}
else if($level != 0 && $category->level > $level){
if(!isset($data[$n]["children"])){
$data[$n]["children"] = array();
}
$data[$parent]["children"][] = $node;
}
else
{
$data[] = $node;
$parent = $n;
}
$level=$category->level;
}
return $data;
}