一级菜单的显示做好了,接下来就应该是二级菜单的显示了,要查询一级菜单下面对应的二级菜单很简单只需要取得一级菜单的id即可,按照我的设计通过点击一级菜单的超链接才能看到子菜单,通过超链接可以把一级菜单的id传给后台控制器,由控制器负责抓取数据和页面跳转
1.控制器代码
[php]
function getChildMenu(){
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
$topImpl=new TopMenuManagerImpl();
//查询用户对应的菜单
$arr=$topImpl->getChildMenuItem($_GET["parentMenu"]);
//通过菜单ID获取菜单名字
$menuname=$topImpl->getMainMenuName($_GET["parentMenu"]);
$smart->assign("child",$arr);
$smart->assign("menuname",$menuname[0]["name"]);
$smart->display("leftmain.tpl");
}
2.TopMenuManagerImpl对应的两个方法,接口代码就省略不写了
[php]
//读取子菜单
public function getChildMenuItem($parentMenu){
$db=new DBUtil();
$conn=$db->getConnection();
$base=new BaseDBOperate();
$sql="select m.* from m_menu m where parentmenu=$parentMenu";
$arr=$base->query($sql,$conn);
$db->close($conn);
return $arr;
}
//读取被点击的主菜单的名称
public function getMainMenuName($parentMenu){
$db=new DBUtil();
$conn=$db->getConnection();
$base=new BaseDBOperate();
$sql="select m.* from m_menu m where id=$parentMenu";
$arr=$base->query($sql,$conn);
$db->close($conn);
return $arr;
}
3.子菜单模板页面
[html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<link rel="stylesheet" type="text/css" href="../../css/style.css">
<link rel="stylesheet" type="text/css" href="../../css/style_m.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<h1><div align="center"><{$menuname}></div></h1>
<tr>
<td valign="top"><table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#ebebeb" class="tableHead2">
<{foreach from=$child item=message}>
<tr>
<td height="21" valign="top" bgcolor="#FFFFFF"><div align="center"><a href="<{$message.url}>" target="mainFrame_s"><{$message.name}></a></div></td>
</tr>
<{/foreach}>
</table></td>
</tr>
</table>
</body>
</html>
到这里二级菜单也显示出来了,这里有人说把一级菜单名称传过去就可以不用再去查询菜单名称了。我这么做是为了保险起见,防止传参后出现乱码
3.菜单管理
菜单管理又分为2块,一块是一级菜单管理,另一块是二级菜单管理,两块的管理都包括增删改查四种操作,首先来看一级菜单的添加操作,下面是添加一级菜单的模板页面
[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript">
function submitCheck(){
var menuname=$("#menuname").val();
if(""==menuname){
alert("菜单名称不能为空");
return false;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加一级菜单</title>
<link rel="stylesheet" href="../../css/EditPage.css" type="text/css"></link>
</head>
<body>
<h1>添加一级菜单</h1>
<form action="../controller/TopMenuController.php" name="forms" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ebebeb" class="black">
<tr>
<td class="lable">菜单名称:</td>
<td>
<input type="text" name="menuname" id="menuname" /><span class="red_star">*</span>
<input type="hidden" name="type" value="createMenu"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" onClick="javaScript:history.back(-1);" value="返回"/>
<input type="submit" value="[新增保存]" onclick="javascript:return submitCheck();"/>
</td>
</tr>
</table>
</form>
</body>
</html>
下面是添加操作的控制器代码
[php]
//添加一级菜单
function addMenu(){
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
$menuManager=new TopMenuManagerImpl();
$result=$menuManager->addMenu($_POST["menuname"], "-1", "-1");
if("1"==$result){
listMenu();
}
}
addMenu方法如下
[php]
//添加一级菜单
public function addMenu($menuName,$url,$parentMenu){
$db=new DBUtil();
$conn=$db->getConnection();
$base=new BaseDBOperate();
$sql="insert into m_menu(name,url,parentmenu) values ('$menuName','$url','$parentMenu')";
$result=$base->otherOperate($sql, $conn);
$db->close($conn);
return $result;
}
下面是菜单列表显示功能也就是上面的那个listMenu,这个方法也在我们的控制器里
[php]
//一级菜单管理
function listMenu(){
$menuManager=new TopMenuManagerImpl();
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
$fenyePage = new fenyePage();
//如果当前页未获取到,则默认为首页
$fenyePage->nowPage = 1;
if (!empty ($_GET['nowPage'])) {
$fenyePage->nowPage = $_GET['nowPage'];
}
$fenyePage->url="TopMenuController.php?type=listMenu";
//设置默认每页显示几条数据
$fenyePage->everyPageRows = 5;
//默认翻页页数
$fenyePage->pageWhole = 3;
$menuManager->menuList($fenyePage);
if(($fenyePage->sumPage)<($fenyePage->pageWhole)){
$fenyePage->pageWhole=$fenyePage->sumPage;
}
//分页后的数据
$smart->assign("page",$fenyePage);
$smart->assign("data",$fenyePage->fenyeArray);
$smart->display("menuList.tpl");
}
看到了吗smarty的确很强大,我们直接可以把分页的数据和分页导航条通过smarty一起分配给模板页面,来看看这个模板页面吧
[html]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>学生管理系统</title>
<link rel="stylesheet" type="text/css" href="../../css/ListPage.css">
<script type="text/javascript" src="../../js/SubmitFun.js"></script>
<script type="text/javascript" src="../../js/jiaoyan.js"></script>
<{include file="../../../css/global.css"}>
</head>
<body>
<form action="TopMenuController.php" method="POST">
<table width="100%" height="20" border="0" cellpadding="0" cellspacing="0">
<tr class="tableHead2">
<td class=t1 align="right" width="50%">
<input type="hidden" name="type" value="deleteMenu" />
<input type="submit" value="[删除]" id="delete" onclick="javaScript:return recordDeletecheck()"/>
</td>
</tr>
</table>
<table style= "TABLE-LAYOUT: fixed;width:100% " border="0" cellpadding="1" cellspacing="1" class="tableMain">
<tr bgcolor="#FFFFFF" class="tableHead">
<th height="21" width="5%" align="left"><input type="checkBox" id="selectRows" onclick="javaScript: return checkChange();"/>选择</th>
<th align="center" width="20%">名称</th>
<th align="center" width="10%">操作</th>
</tr>
<{foreach from=$data item=message}>
<tr bgcolor="#FFFFFF">
<td height="21" align="left">
<input type="checkBox" name="selectIDs[]" id="news<{$message.id}>" value="<{$message.id}>" />
</td>
<td >
<div>
<a href="TopMenuController.php?type=edit&id=<{$message.id}>"><{$message.name}></a>
</div>
</td>
<td nowrap="nowrap">
<div align="center">
<a href="TopMenuController.php?type=manageChildMenu&id=<{$message.id}>">管理二级菜单</a>
<a href="TopMenuController.php?type=addChildMenuUI&id=<{$message.id}>">添加二级菜单</a>
</div>
</td>
</tr>
<{/foreach}>
</table>
</form>
<div style="float:left;"><{$page->showNavigate()}></div>
</body>
</html>
来看看这个页面长什么样子吧
这里使用smarty要注意一个问题,因为分页导航条是通过smarty分配过去的,这种分页导航条一般都有css样式。我们要考虑的问题就是能不能再列表页面直接引入css呢,答案是不可以的,因为css是针对分页的但是分页又是通过smary分配过去的因此css文件也需要被smarty解析才可以,这里就需要使用smarty提供的包含指令了
<{include file="../../../css/global.css"}>
这个css文件写法上页有讲究,最前面要写上<style type="text/css">最末尾要加上</style>,如下所示
[css]
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
ul,ol,li{
list-style-type:none;
}
.fenye_ul {
padding: 0;
margin-left:20px;
float: left;
}
.fenye_ul li {
list-style-type:none;
float: left;
border: 1px solid #6699cc;
text-align: center;
margin-left: 3px;
padding: 2px 5px;
font-size: 10px;
list-style: none;
}
.fenye_ul .btn {
float: left;
border: 1px solid #6699cc;
text-align: center;
margin-left: 2px;
font-size: 10px;
}
</style>
接下来就是一级菜单的删除和修改了,这里只演示修改操作,修噶操作是通过点击菜单名称链接过去的,链接需要传一个菜单ID过去,之后根据ID查询出单个的菜单,把信息重新显示在表单中
点击链接后调用的控制器代码如下
[php]
//一级菜单编辑
function editMenu(){
$id=$_GET["id"];
$menuManager=new TopMenuManagerImpl();
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
$result=$menuManager->menuEdit($id);
$smart->assign("menuItem",$result);
$smart->display("editMenu.tpl");
}
下面是实现类里的menuEdit方法
[php]
//菜单编辑功能
public function menuEdit($id){
$db=new DBUtil();
$conn=$db->getConnection();
$base=new BaseDBOperate();
$sql="select A.* from m_menu A where A.id=$id";
$arr=$base->query($sql, $conn);
$db->close($conn);
return $arr;
}
接下来就是编辑页面了,模板文件如下
[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
});
</script>
<script type="text/javascript">
function submitCheck(){
var menuname=$("#menuname").val();
if(""==menuname){
alert("菜单名称不能为空");
return false;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑一级菜单</title>
<link rel="stylesheet" href="../../css/EditPage.css" type="text/css"></link>
</head>
<body>
<h1>编辑一级菜单</h1>
<form action="../controller/TopMenuController.php" method="post" >
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#ebebeb" class="black">
<tr>
<td class="lable">用户名:</td>
<td>
<input type="hidden" name="type" value="updateMenu"/>
<input type="hidden" name="id" value="<{$menuItem[0].id}>"/>
<input type="text" name="menuname" id="menuname" value="<{$menuItem[0].name}>"/><span class="red_star">*</span>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" onClick="javaScript:history.back(-1);" value="返回"/>
<input type="submit" value="[编辑保存]" onclick="javascrip:return submitCheck();"/>
</td>
</tr>
</table>
</form>
</body>
</html>
编辑页面的添加页面的差别就是表单里是否有值,这一块相信难度也不大,接下来是修改数据库表数据的操作,还是先上控制器代码
[php]
//更新菜单
function updateMenu(){
$id=$_POST["id"];
$menuManager=new TopMenuManagerImpl();
$result=$menuManager->menuUpdate($_POST["id"], $_POST["menuname"], "-1", "-1");
if($result=="1"){
listMenu();
}
}
下面就是这个menuUpdate方法了
[php]
//修改一级菜单
public function menuUpdate($id,$name,$url,$parentMenu){
$db=new DBUtil();
$conn=$db->getConnection();
$base=new BaseDBOperate();
$sql="update m_menu set name='$name',url='$url',parentmenu='$parentMenu' where id=$id";
$result=$base->otherOperate($sql, $conn);
$db->close($conn);
return $result;
}
这个模块是不是也很简单呢得意