function on_trview(trname,enforce)
{
<%
for(int i=0; i<topCategories.size(); i++) {
Category c = topCategories.get(i);
%>
document.all.pma_<%=c.getId()%>.style.display="none";
<%
}
%>
if (enforce=="in")
{
//eval里的代码当做javascript代码执行一遍
eval("document.all.pma_"+trname+".style.display=''");
//eval("alert()");
//ruby on rails
}
}
//获取顶级菜单
private List<Category> getTopCategories(List<Category> categories) {
List<Category> topCategories = new ArrayList<Category>();
for (int i = 0; i < categories.size(); i++) {
Category c = categories.get(i);
if (c.getGrade() == 1) {
topCategories.add(c);
}
}
return topCategories;
}
//获取相应的子菜单列表
private List<Category> getChilds(Category parent, List<Category> categories) {
List<Category> childs = new ArrayList<Category>();
for (int i = 0; i < categories.size(); i++) {
Category c = categories.get(i);
if (c.getPid() == parent.getId()) {
childs.add(c);
}
}
return childs;
}
//获取子菜单
private String getChildsStr(List<Category> childs) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < childs.size(); i++) {
Category c = childs.get(i);
buf.append(
"<a href='ShowProducts.jsp?categoryId=" + c.getId() + "'>")
.append(c.getName()).append("</a>").append("-");
}
return buf.toString();
}
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" background="images/topbg.gif" valign="middle"width="897">
<table align="left" border="0" cellpadding="0" cellspacing="0"width="713">
<tr class="style1" align="center">
<td align="left">
<!-- 显示第一层目录 -->
<%
for (int i = 0; i < topCategories.size(); i++) {
Category c = topCategories.get(i);
%>
<a href="" onMouseOver="on_trview(<%=c.getId()%>,'in')">
<!--<img src="images/00.gif" name="Image0" border="0">-->
<span style="color: #FFFFFF"><%=c.getName()%>
</span>
</a>
<%
}
%>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td background="images/topbg2.gif" height="25">
<table style="border-collapse: collapse;" class="twoji"
border="0" cellpadding="0" cellspacing="0" width="100%">
<%
for (int i = 0; i < topCategories.size(); i++) {
Category parent = topCategories.get(i);
List<Category> childs = getChilds(parent, categories);
%>
<tr id="pma_<%=parent.getId()%>" style="display: none;">
<td style="position: relative; left: 0pt;" height="25"
width="100%">
<%=getChildsStr(childs)%>
</td>
</tr>
<%
}
%>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>