javascript从数据库获取数据后,生成EasyUI树

不用管是什么数据库,php还是java语言

只要数据库表字段是有自身指向的结构的都行。

废话少说,直接上代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MyOA</title>
<link rel="stylesheet" type="text/css" href="http://jeasyui.com/easyui/themes/material-teal/easyui.css">
<link rel="stylesheet" type="text/css" href="http://jeasyui.com/easyui/themes/icon.css">
</head>
<body>
<ul id="tt" class="easyui-tree" data-options="data:menuData"></ul>
</body>
<script type="text/javascript" src="http://jeasyui.com/easyui/jquery.min.js"></script>
<script type="text/javascript" src="http://jeasyui.com/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
function makeEasyTree(data){
	if(!data)
		return [];
	var _newData = []; //最终返回结果
	var _treeArray = {}; //记录一级节点
	var _root = 1; //最顶层fid
	var _idKey = "id"; //主键的键名
	var _fidKey = "fid"; //父ID的键名
	_getChildren(_root);
	function _getChildren($root){
		var $children = [];
		for (var i in data){
			if($root == data[i][_fidKey]){
				data[i]["children"] = _getChildren(data[i][_idKey]);
				$children.push(data[i]);
			}
			//只要一级节点
			if(_root == data[i][_fidKey] && !_treeArray[data[i][_idKey]]){
				_treeArray[data[i][_idKey]] = data[i];
				_newData.push(data[i]);
			}
		}
		return $children;
	}
	return _newData;
}
 
var menuData =  [
{"id":"2","fid":"1","text":"系统设置","nodeUrl":"Admin"},
{"id":"5","fid":"1","text":"笔记本","nodeUrl":"Admin"},
{"id":"3","fid":"2","text":"系统角色管理","nodeUrl":"Admin&m=Sys&a=manageRole"},
{"id":"4","fid":"2","text":"系统菜单管理","nodeUrl":"Admin&m=Sys&a=manageNode"},
{"id":"8","fid":"2","text":"用户管理","nodeUrl":"Admin&m=Sys&a=manageUser"},
{"id":"6","fid":"5","text":"笔记列表","nodeUrl":"Admin&m=LjbNote"},
{"id":"7","fid":"5","text":"添加笔记","nodeUrl":"Admin&m=LjbNote&a=saveNote"},
{"id":"9","fid":"7","text":"管理分类","nodeUrl":"Admin&m=LjbNote&a=manageType"},
{"id":"10","fid":"9","text":"test","nodeUrl":""}];
menuData = makeEasyTree(menuData);
</script>
</html>

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值