当前位置:首页 > 代码 > 正文

树形菜单代码(树形菜单数据库)

admin 发布:2022-12-19 18:16 110


本篇文章给大家谈谈树形菜单代码,以及树形菜单数据库对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

PHP递归树形菜单

可以参考以下代码,但需要把你自己的数据库链接,表等改一下就可以了

html

head

link href='style.css' rel=stylesheet

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

script language="JavaScript" src="TreeMenu.js"/script

/head

body

?php

//基本变量设置

$GLOBALS["ID"] =1; //用来跟踪下拉菜单的ID号

$layer=1; //用来跟踪当前菜单的级数

//连接数据库

$Con=mysql_connect("localhost","root","1234");

mysql_select_db("wiki");

//提取一级菜单

$sql="SELECT * FROM wiki where pid=0";

$result=mysql_query($sql,$Con);

//如果一级菜单存在则开始菜单的显示

if(mysql_num_rows($result)0) ShowTreeMenu($Con,$result,$layer,$ID);

//=============================================

//显示树型菜单函数 ShowTreeMenu($con,$result,$layer)

//$con:数据库连接

//$result:需要显示的菜单记录集

//layer:需要显示的菜单的级数

//=============================================

function ShowTreeMenu($Con,$result,$layer)

{

//取得需要显示的菜单的项目数

$numrows=mysql_num_rows($result);

//开始显示菜单,每个子菜单都用一个表格来表示

echo "table cellpadding='0' cellspacing='0' border='0'";

for($rows=0;$rows$numrows;$rows++)

{

//将当前菜单项目的内容导入数组

$menu=mysql_fetch_array($result);

//提取菜单项目的子菜单记录集

$sql="select * from wiki where pid=$menu[cid]";

$result_sub=mysql_query($sql,$Con);

echo "tr";

//如果该菜单项目有子菜单,则添加JavaScript onClick语句

if(mysql_num_rows($result_sub)0)

{

echo "td width='20'img src='folder.gif' border='0'/td";

echo "td class='Menu' onClick='javascript:ShowMenu(Menu".$GLOBALS["ID"].");'";

}

else

{

echo "td width='20'img src='file.gif' border='0'/td";

echo "td class='Menu'";

}

//如果该菜单项目没有子菜单,并指定了超级连接地址,则指定为超级连接,

//否则只显示菜单名称

//if($menu[url]!="")

//echo "a href='$menu[cid]'$menu[name]/a";

//else

echo $menu['name'];

echo "

/td

/tr

";

//如果该菜单项目有子菜单,则显示子菜单

if(mysql_num_rows($result_sub)0)

{

//指定该子菜单的ID和style,以便和onClick语句相对应

echo "tr id=Menu".$GLOBALS["ID"]++." style='display:none'";

echo "td width='20' /td";

echo "td";

//将级数加1

$layer++;

//递归调用ShowTreeMenu()函数,生成子菜单

ShowTreeMenu($Con,$result_sub,$layer);

//子菜单处理完成,返回到递归的上一层,将级数减1

$layer--;

echo "/td/tr";

}

//继续显示下一个菜单项目

}

echo "/table";

}

?

/body

/html

JSP动态树形菜单,菜单项从数据库中获得

jsp动态树形菜单须用到递归算法,比如在数据库有张表,parent表,parent的字段有id,name,depth,leve,ID自增,depth设置为级数,如这条数据最大,为0,如为字菜单就为1,而leve就指定它父节点的id,给段代码自己可以摸索下 public Vector getModuleTree()

{

Vector pclass = new Vector();

try

{

stmt =con.createStatement();

String sql = "select * from Module where parentid = 0";

rs = stmt.executeQuery(sql);

Module cvo = null;

while(rs.next())

{

cvo = new Module();

cvo.setModule_id(rs.getInt("Module_id"));

cvo.setModule_name(rs.getString("Module_name"));

cvo.setModule_url(rs.getString("Module_url"));

cvo.setParentid(rs.getInt("parentid")); cvo.setRootid(rs.getInt("rootid")); cvo.setDepth(rs.getInt("depth")); pclass.add(cvo);

}

for (int i = 0; i pclass.size(); i++)

{

Module pcvo = (Module) pclass.get(i);

ShowTreeMenu(pcvo);

}

con.commit(); } catch (SQLException e)

{

e.printStackTrace();

} finally

{

try

{

if(rs!=null)

{

rs.close();

}

if(stmt!=null)

{

stmt.close();

}

if(con!=null)

{

con.close();

}

}

catch (SQLException e)

{

e.printStackTrace();

}

}

return classList;

}

public void ShowTreeMenu(Module c)

{

Module ccvo = null;

String sql = "select * from Module where parentid = " + c.getModule_id();

Vector cclass = new Vector();

try

{

Module cvotemp;

stmt =con.createStatement();

rs = stmt.executeQuery(sql);

while(rs.next())

{

cvotemp = new Module();

cvotemp.setModule_id(rs.getInt("Module_id"));

cvotemp.setModule_name(rs.getString("Module_name"));

cvotemp.setModule_url(rs.getString("Module_url"));

cvotemp.setParentid(rs.getInt("parentid")); cvotemp.setRootid(rs.getInt("rootid")); cvotemp.setDepth(rs.getInt("depth")); cclass.add(cvotemp);

}

System.out.println(cclass.size()+"(((((((((((((((((((((((((9");

if (cclass.size() 0)

{

c.setHasChild("have");

classList.add(c);

for (int j = 0; j cclass.size(); j++)

{

ccvo = (Module) cclass.get(j);

ShowTreeMenu(ccvo);

} } else

{

classList.add(c);

}

} catch (SQLException e)

{

e.printStackTrace();

}

}

怎么用jquery 写个树形菜单

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

titlejquery的树形菜单代码 /title

style

body { font-family:"宋体"; font-size: 12px; line-height: 1.5em; color:#7FB0C8; padding:0; margin:0; background: #336699;}

ul,ol,li,dl,dt,dd { margin:0; padding:0; list-style-type:none;}

h1,h2,h3,form,input,iframe,span { margin:0; padding:0;}

a { color:#7FB0C8;}

a:link {color: #7FB0C8; TEXT-DECORATION: none;}

a:visited {color: #7FB0C8; TEXT-DECORATION: none;}

a:hover {color: #fff; TEXT-DECORATION: none;}

.white { color:#fff;}

.white a:link {color: #fff; TEXT-DECORATION: none;}

.white a:visited {color: #fff; TEXT-DECORATION: none;}

.white a:hover {color: #73E1F5; TEXT-DECORATION: none;}

/* 树形菜单开始 */

.close { float:right; clear:right; font-size:12px; font-weight:normal; cursor:pointer; padding-right:10px;}

.title { font-size:14px; color:#fff; margin-bottom:10px; padding-left:5px; width:290px;}

.menu { width:290px; height:330px; overflow-y:auto; overflow-x:hidden; margin-bottom:10px;}

.menu { SCROLLBAR-FACE-COLOR: #002537; SCROLLBAR-HIGHLIGHT-COLOR: #002537; SCROLLBAR-SHADOW-COLOR: #0E6893; SCROLLBAR-3DLIGHT-COLOR: #0E6893; SCROLLBAR-ARROW-COLOR: #fff; SCROLLBAR-TRACK-COLOR: #053d58;SCROLLBAR-DARKSHADOW-COLOR: #002537; SCROLLBAR-BASE-COLOR: #01152a;}

.l1 { background:#000; font-size:13px; padding:5px 0 0 30px; height:20px; margin-bottom:5px; cursor:pointer;}

.slist { margin:0 0 5px 0; display:none;}

.l2 { padding:0 0 0 35px; font-size:13px;}

.l2 a { padding:6px 0 0 5px; width:230px; height:21px; display:block;}

.currentl2 a,.l2 a:hover { background:#1E5A82; color:#fff;}

.sslist { background:#156890; width:235px; overflow:hidden; margin:0 0 5px 35px; display:none;}

.l3 a { padding:6px 0 0 5px; width:230px; height:20px; display:block;}

.currentl3 a,.l3 a:hover { color:#fff; font-weight:bold;}

/style

script type="text/javascript" src="/ajaxjs/jquery1.3.2.js"/script

script

// 树状菜单

$(document).ready(function(){

$(".l1").toggle(function(){

$(".slist").animate({height: 'toggle', opacity: 'hide'}, "slow");

$(this).next(".slist").animate({height: 'toggle', opacity: 'toggle'}, "slow");

},function(){

$(".slist").animate({height: 'toggle', opacity: 'hide'}, "slow");

$(this).next(".slist").animate({height: 'toggle', opacity: 'toggle'}, "slow");

});

$(".l2").toggle(function(){

$(this).next(".sslist").animate({height: 'toggle', opacity: 'toggle'}, "slow");

},function(){

$(this).next(".sslist").animate({height: 'toggle', opacity: 'toggle'}, "slow");

});

$(".l2").click(function(){

$(".l3").removeClass("currentl3");

$(".l2").removeClass("currentl2");

$(this).addClass("currentl2");

});

$(".l3").click(function(){

$(".l3").removeClass("currentl3");

$(this).addClass("currentl3");

});

$(".close").toggle(function(){

$(".slist").animate({height: 'toggle', opacity: 'hide'}, "fast");

$(".sslist").animate({height: 'toggle', opacity: 'hide'}, "fast");

},function(){

$(".slist").animate({height: 'toggle', opacity: 'show'}, "fast");

$(".sslist").animate({height: 'toggle', opacity: 'show'}, "fast");

});

});

/script

/head

body

h1 class="title"span class="close"全部收起/展开/span树形菜单/h1

div class="menu"

h1 class="l1"一级菜单/h1

div class="slist"

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

ul class="sslist"

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

/ul

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

ul class="sslist"

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

li class="l3"a href="javascript:function()"·三级菜单/a/li

/ul

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

/div

h1 class="l1"一级菜单/h1

div class="slist"

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

/div

h1 class="l1"一级菜单/h1

div class="slist"

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

h2 class="l2"a href="javascript:function()"二级菜单/a/h2

/div

/div

/body

/html

关于树形菜单代码和树形菜单数据库的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/17038.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载