一、代码
1、后端php代码class suite
{
public function suiteList($projecId){
$projecId = 670855;
$this->smarty->assign('projecId',$projecId);
$this->smarty->display($this->smarty->tlTemplateCfg->template_dir .'suiteList.tpl');
}
public function suiteListAjax(){
$projecId = $_REQUEST['projecId'];
$parentId = isset($_REQUEST['id'])?$_REQUEST['id']:0;
$opt = array('node_type_id'=>4);
$rootSuite = $this->tree->rootNodeByProject($parentId,$projecId,$opt);
foreach ($rootSuite as $key=>$value){
$sonNodeLength = $this->tree->isParent($value['id']);
if($sonNodeLength>0){
$rootSuite[$key]['isParent'] = 'true';
}else{
$rootSuite[$key]['isParent'] = 'false';
}
}
echo json_encode($rootSuite);
}
}
2、前端代码
树结构菜单管理
var projecId = {$projecId} //后端php传递的项目id
var setting = {
view: {
selectedMulti: false
},
//异步配置
async: {
enable: true, //是否开启
type: "post", //ajax数据提交格式
url:"casesIndex.php?c=suite&f=suiteListAjax", //后台地址
autoParam:["id", "name=n", "level=lv"],
otherParam:{"projecId":projecId},
dataFilter: filter
},
callback: {
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i=0, l=childNodes.length; i
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
//初始化节点
$(document).ready(function(){
$.fn.zTree.init($("#modulesTree"), setting);
});
二、说明
ztree是基于jQuery的,所以使用之前要先引入jQuery库,最基础的使用是引入如下3个文件:
zTree_v3/css/zTreeStyle/zTreeStyle.css (自带样式)
zTree_v3/js/jquery-1.4.4.min.js(ztree官方提供的和当前版本对应的jQuery库)
zTree_v3/js/jquery.ztree.core.js(ztree 核心库)
前端调用执行
1、$.fn.zTree.init初始化节点
2、filter(treeId, parentNode, childNodes) 把php返回的json格式数据转化为数组格式
.....