easyUI官方组合树ComboTree代码:

tree_data1.json:
[{
"id":1,
"text":"My Documents",
"children":[{
"id":11,
"text":"Photos",
"state":"closed",
"children":[{
"id":111,
"text":"Friend"
},{
"id":112,
"text":"Wife"
},{
"id":113,
"text":"Company"
}]
},{
"id":12,
"text":"Program Files",
"children":[{
"id":121,
"text":"Intel"
},{
"id":122,
"text":"Java",
"attributes":{
"p1":"Custom Attribute1",
"p2":"Custom Attribute2"
}
},{
"id":123,
"text":"Microsoft Office"
},{
"id":124,
"text":"Games",
"checked":true
}]
},{
"id":13,
"text":"index.html"
},{
"id":14,
"text":"about.html"
},{
"id":15,
"text":"welcome.html"
}]
}]
思路:想要实现动态加载,必须从数据库返回具有父子层级关系的数据格式,(涉及到三级菜单以上的话要使用多级关联查询,)才能正确显示。
数据表Xzq:id,name,parentId (菜单为自关联)
1,中国,0
2,安徽,1
3,浙江,1
Thinkphp代码实现:
模型层:
<?php
namespace Home\Model;
use Think\Model\RelationModel;
class XzqModel extends RelationModel{
protected $_link = array(
‘children‘ =>array(
‘mapping_type‘ => self::HAS_MANY, //关联关系
‘class_name‘ => ‘Category‘, //要关联的模型类名
‘parent_key‘ => ‘parentId‘, //自引用关联的关联字段
‘mapping_fields‘=>‘id,name as text,parentId‘, //关联要查询的字段
‘relation_deep‘=>true, //支持多级查询设置
)
);
控制器:
$result=$category->field(‘id,name as text‘)->relation(true)
->where(‘companyUserId=‘.session(‘id‘).‘ and parentId=0‘)->select();
这样得到的关联数组即是一个符合easyUI组合树ComboTree要求的数据格式。
thinkphp关联查询实现动态加载easyUI组合树ComboTree
原文:http://www.cnblogs.com/ahguSH/p/5093432.html