• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

zTree+php动态加载menu

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<title>index.html</title>

<meta charset="utf-8">

<link rel="stylesheet" href="zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="zTree_v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"  src="zTree_v3/js/jquery.ztree.core-3.5.js"></script>
<style>
body {
	background-color: white;
	margin: 0;
	padding: 0;
	text-align: center;
}

div,p,table,th,td {
	list-style: none;
	margin: 0;
	padding: 0;
	color: #333;
	font-size: 12px;
	font-family: dotum, Verdana, Arial, Helvetica, AppleGothic, sans-serif;
}

#testIframe {
	margin-left: 10px;
}
</style>

<SCRIPT type="text/javascript">
	var zTree;
	var demoIframe;
	var menuNode;
	var setting = {
		view : {
			dblClickExpand : false,
			showLine : true,
			selectedMulti : false
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "pId",
				rootPId : ""
			}
		},
		callback : {
			beforeClick : function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);
					return false;
				} else {
					demoIframe.attr("src", treeNode.file);
					return true;
				}
			}
		}
	};

     
	$(function(){
		$.post("src/dao/conn.php", function(data){
			menuNode=$.parseJSON(data);
			for(var i=0;i<menuNode.length;i++){
				 menuNode[i].name=decodeURI(menuNode[i].name);
			}
			var t = $("#tree");
			t = $.fn.zTree.init(t, setting, menuNode);
			demoIframe = $("#testIframe");
			demoIframe.bind("load", loadReady);
			console.info(menuNode);
		});
	});
	   
	function loadReady() {
		var bodyH = demoIframe.contents().find("body").get(0).scrollHeight, 
		htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
		maxH = Math.max(bodyH, htmlH),
		minH = Math.min(bodyH, htmlH), 
		h = demoIframe.height() >= maxH ? minH : maxH;
		if (h < 530)
			h = 530;
		demoIframe.height(h);
	}
</SCRIPT>

</head>

<body>
	<TABLE border=0 height=600px align=left>
		<TR>
			<TD width=260px align=left valign=top style="BORDER-RIGHT: #999999 1px dashed">
				<ul ></ul>
			</TD>
			<TD width=770px align=left valign=top><IFRAME ID="testIframe"
					Name="testIframe" FRAMEBORDER=0 SCROLLING=AUTO width=100%
					height=600px SRC="php/1.html"></IFRAME></TD>
		</TR>
	</TABLE>
</body>
</html>

2.后台conn.php代码

<?php
$mysqli  = new  mysqli ( 'localhost' ,  'root' ,  'root' ,  'php' );
mysqli_query($mysqli, "set names gbk");
if ( $mysqli -> connect_error ) {
    die(  $mysqli -> connect_errno + $mysqli -> connect_error );
}
$sql='select * from menu';
$res=mysqli_query($mysqli, $sql);
$array=array();
while ($row=mysqli_fetch_row($res)){
	$menu=array(
		"id"=>$row[1],
		"pId"=>$row[0],
		"name"=>urlencode(iconv('gb2312','utf-8',$row[2])),
		"open"=>$row[3],
		"file"=>$row[4],	
	);
	array_push($array, $menu);
}
echo json_encode($array);

 $mysqli -> close ();
	
?>

3.数据库表内容

mysql> select * from menu;
+------+-----+---------+------+------------+
| pId  | id  | name    | open | file       |
+------+-----+---------+------+------------+
| NULL |   1 | 最简单        |    0 | NULL       |
| NULL |   2 | 简单        |    0 | NULL       |
| NULL |   3 | 超简单        | NULL | NULL       |
|    2 |  21 | 案例3       | NULL | php/1.html |
|    1 | 101 | 简单        | NULL | NULL       |
|    1 | 102 | 案例2       | NULL | php/2.html |
|  101 | 201 | 案例1       | NULL | php/1.html |
|    3 | 301 | 超简单1       | NULL | php/1.html |
+------+-----+---------+------+------------+
8 rows in set (0.00 sec)

4.结果



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap