樹型菜單在很多桌面應(yīng)用系統(tǒng)中都有非常廣泛的應(yīng)用,其主要優(yōu)點(diǎn)是結(jié)構(gòu)清晰,利于使用者非常清楚的知道目前自己所在的位置。但在web上樹型菜單的應(yīng)用因?yàn)闆]有理想的現(xiàn)成組件可以拿過來直接使用,所以一般的情況下,程序員主要是通過javascript來實(shí)現(xiàn)一些簡單的樹型結(jié)構(gòu)菜單,但這些菜單往往都是事先定好各菜單項(xiàng)目,以及各菜單項(xiàng)目之間的層次關(guān)系,不利于擴(kuò)充,一旦需要另一個(gè)菜單結(jié)構(gòu)時(shí),往往還需要重新編寫,因此使用起來不是很方便。
經(jīng)過對(duì)函數(shù)遞歸的研究,我發(fā)現(xiàn)這種樹型菜單可以通過遞歸函數(shù),使樹型菜單的顯示實(shí)現(xiàn)動(dòng)態(tài)變化,并沒有級(jí)數(shù)的限制。下面就是我用php,mysql,javascript寫的一個(gè)動(dòng)態(tài)樹型菜單的處理代碼,如果大家有興趣的話,就和我一起來看看我是如何實(shí)現(xiàn)的吧:)
首先,我們需要一個(gè)數(shù)據(jù)庫,在這個(gè)數(shù)據(jù)庫中,我們建立以下一張表:
create table menu (
id tinyint(4) not null auto_increment,
parent_id tinyint(4) default '0' not null,
name varchar(20),
url varchar(60),
primary key (id)
);
這張表中
id 為索引
parent_id 用來保存上一級(jí)菜單的id號(hào),如果是一級(jí)菜單則為0
name 為菜單的名稱,也就是要在頁面上顯示的菜單內(nèi)容
url 如果某菜單為末級(jí)菜單,則需要指定該連接的url地址,這個(gè)字段就是用來保存此地址的,其他非末級(jí)菜單,該字段為空
好了,數(shù)據(jù)庫有了,你就可以添加一些記錄了,下面是我做測試的時(shí)候,使用的一些記錄:
insert into menu values ( '1', '0', '人事管理', '');
insert into menu values ( '2', '0', '通訊交流', '');
insert into menu values ( '3', '1', '檔案管理', '');
insert into menu values ( '4', '1', '考勤管理', 'http://localhost/personal/attendance.php');
insert into menu values ( '5', '2', '通訊錄', '');
insert into menu values ( '6', '2', '網(wǎng)絡(luò)會(huì)議', '');
insert into menu values ( '7', '3', '新增檔案', 'http://localhost/personal/add_achive.php');
insert into menu values ( '8', '3', '查詢檔案', 'http://localhost/personal/search_archive.php');
insert into menu values ( '9', '3', '刪除檔案', 'http://localhost/personal/delete_archive.php');
insert into menu values ( '10', '5', '新增通訊記錄','http://localhost/communication/add_address.php');
insert into menu values ( '11', '5', '查詢通訊記錄', http://localhost/communication/search_address.php');
insert into menu values ( '12', '5', '刪除通訊記錄', http://localhost/communication/delete_address.php');
insert into menu values ( '13', '6', '召開會(huì)議', 'http://localhost/communication/convence_meeting.php');
insert into menu values ( '14', '6', '會(huì)議查詢', 'http://localhost/communication/search_meeting.php');
在添加記錄的時(shí)候,一定要注意,非一級(jí)菜單的parent_id一定要指定為上級(jí)菜單的id號(hào),否則你的菜單是不會(huì)顯示出來的:)
好了!有了數(shù)據(jù)庫,下面就是通過php,javascript把菜單從數(shù)據(jù)庫中讀出來,并顯示出來了:)
1、javascript腳本: function showmenu(menuid)
{
if(menuid.style.display=="none")
{
menuid.style.display="";
}
else
{
menuid.style.display="none";
}
}
這個(gè)腳本很簡單,就是用來響應(yīng)點(diǎn)擊某個(gè)菜單被點(diǎn)擊的事件的。
2、css文件: <!-- 表格樣式 -->
td {
font-family: "verdana", "宋體"; font-size: 12px; line-height: 130%; letter-spacing:1px
}
<!-- 超級(jí)連接樣式 -->
a:link {
color: #990000; font-family: "verdana", "宋體"; font-size: 12px; text-decoration: none; letter-spacing:1px
}
a:visited {
color: #990000; font-family: "verdana", "宋體"; font-size: 12px; text-decoration: none; letter-spacing:1px
}
a:active {
color: #990000; font-family: "verdana", "宋體"; font-size: 12px; text-decoration: none; letter-spacing:1px
}
a:hover {
color: #ff0000; font-family: "verdana", "宋體"; font-size: 12px; text-decoration: underline; letter-spacing:1px
}
<!-- 其他樣式 -->
.menu {
color:#000000; font-family: "verdana", "宋體"; font-size: 12px; cursor: hand
}
定義了一些基本的樣式信息,比如字體,顏色,超級(jí)連接的樣式等,如果你想改變樣式的話,只要修改這里就行了!
3、下面就是我的php頁面了!
<html>
<head>
<link href='style.css' rel=stylesheet>
<script language="javascript" src="treemenu.js"></script>
</head>
<body>
<?php
//基本變量設(shè)置
$globals["id"] =1; //用來跟蹤下拉菜單的id號(hào)
$layer=1; //用來跟蹤當(dāng)前菜單的級(jí)數(shù)
//連接數(shù)據(jù)庫
$con=mysql_connect("localhost","root","");
mysql_select_db("work");
//提取一級(jí)菜單
$sql="select * from menu where parent_id=0";
$result=mysql_query($sql,$con);
//如果一級(jí)菜單存在則開始菜單的顯示
if(mysql_num_rows($result)>0) showtreemenu($con,$result,$layer,$id);
//=============================================
//顯示樹型菜單函數(shù) showtreemenu($con,$result,$layer)
//$con:數(shù)據(jù)庫連接
//$result:需要顯示的菜單記錄集
//layer:需要顯示的菜單的級(jí)數(shù)
//=============================================
function showtreemenu($con,$result,$layer)
{
//取得需要顯示的菜單的項(xiàng)目數(shù)
$numrows=mysql_num_rows($result);
//開始顯示菜單,每個(gè)子菜單都用一個(gè)表格來表示
echo "<table cellpadding='0' cellspacing='0' border='0'>";
for($rows=0;$rows<$numrows;$rows++)
{
//將當(dāng)前菜單項(xiàng)目的內(nèi)容導(dǎo)入數(shù)組
$menu=mysql_fetch_array($result);
//提取菜單項(xiàng)目的子菜單記錄集
$sql="select * from menu where parent_id=$menu[id]";
$result_sub=mysql_query($sql,$con);
echo "<tr>";
//如果該菜單項(xiàng)目有子菜單,則添加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'>";
}
//如果該菜單項(xiàng)目沒有子菜單,并指定了超級(jí)連接地址,則指定為超級(jí)連接,
//否則只顯示菜單名稱
if($menu[url]!="")
echo "<a href='$menu[url]'>$menu[name]</a>";
else
echo $menu[name];
echo "
</td>
</tr>
";
//如果該菜單項(xiàng)目有子菜單,則顯示子菜單
if(mysql_num_rows($result_sub)>0)
{
//指定該子菜單的id和style,以便和onclick語句相對(duì)應(yīng)
echo "<tr id=menu".$globals["id"]++." style='display:none'>";
echo "<td width='20'> </td>";
echo "<td>";
//將級(jí)數(shù)加1
$layer++;
//遞歸調(diào)用showtreemenu()函數(shù),生成子菜單
showtreemenu($con,$result_sub,$layer);
//子菜單處理完成,返回到遞歸的上一層,將級(jí)數(shù)減1
$layer--;
echo "</td></tr>";
}
//繼續(xù)顯示下一個(gè)菜單項(xiàng)目
}
echo "</table>";
}
?>
</body>
</html>
在上面的php頁面里面,我定義了一個(gè)函數(shù)showtreemenu(),通過這個(gè)函數(shù)的調(diào)用,會(huì)從數(shù)據(jù)庫中遞歸的調(diào)出每個(gè)菜單項(xiàng)目,并顯示在頁面上了:)