如何实现无限级动态树型菜单呢,用xmlhtml和xtree.js。

clei1005 2004-09-08 02:09:03
rt
...全文
590 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
faithwind 2005-09-08
正在看。
回复
cgliang00 2004-09-14
新手想问一下,阿信的XML的目录树如何使用,比如框架格式,左边是导航条,右边显示内容
回复
gaochao79 2004-09-09
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<% request.setCharacterEncoding("gb2312"); //解决乱码的办法%>
<!--
数据库结构:
库名:com
表名:tree
CREATE TABLE [dbo].[tree] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[parentid] [int] NOT NULL ,
[message] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
)
五张小图片:
加号:plus.gif
减号:minus.gif
打开的文件夹:openfold.gif
关闭的文件夹:closedfold.gif
白板:white.gif
-->
<%!//方便起见这里就不写成javabean了
class cn {//连接数据库,这里以MS-SQL为例
String jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//jdbc驱动
String connectionString="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=com";//数据库连接字符串
String user="sa";//数据库用户名
String pass="pcc";//数据库密码
Connection conn=null;
ResultSet rs=null;

public cn() {
try {
Class.forName(jdbcDriver);
} catch(ClassNotFoundException e) {
System.err.println(e.toString());
}
}

public ResultSet executeQuery(String sql) {
rs=null;
try {
conn=DriverManager.getConnection(connectionString,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e) {
System.err.println(e.toString());
}
return rs;
}

public void executeUpdate(String sql) {
try {
conn=DriverManager.getConnection(connectionString,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException e) {
System.err.println(e.toString());
}
}
}

class tree {
cn conn=new cn();
public void init(javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request) throws Exception {
out.println("<title>用jsp种树</title>");
dowith(request);
buildTree(out,0,0);//初始调用
}

private void dowith(javax.servlet.http.HttpServletRequest request) {
if(request.getParameter("parentid")==null||request.getParameter("parentid").equals(""))return;
String action=request.getParameter("action");
if(action.equals("add"))
conn.executeUpdate("insert into tree(parentid,message) values('"+request.getParameter("parentid")+"','"+request.getParameter("message")+"')");
else if(action.equals("delete"))
conn.executeUpdate("delete from tree where id="+request.getParameter("parentid")+" or parentid="+request.getParameter("parentid"));
}

public void buildTree(javax.servlet.jsp.JspWriter out,int parentid,int level) throws Exception {
level++;
ResultSet rs=conn.executeQuery("select * from tree where parentid="+parentid+" order by id");
while(rs.next()) {
out.println("<div>");
for(int i=0;i<level-1;i++)
out.print("<img src=\"white.gif\"> ");
if(has_child(rs.getInt("id"))) {
out.print("<img alt=\"展开\" style=\"cursor:hand;\" onclick=\"myClick('"+rs.getInt("id")+"');\" id=\"img"+rs.getInt("id")+"\" src=\"plus.gif\"> <img id=\"im"+rs.getInt("id")+"\" src=\"closedfold.gif\"> ");
out.print("<span onclick=\"myClick1('"+rs.getInt("id")+"');\" style=\"cursor:default;\" id=\"span"+rs.getInt("id")+"\">"+rs.getString("message")+" id="+rs.getInt("id")+"</span>");
out.println("<div style=\"display:none;\" id=\"div"+rs.getInt("id")+"\">");
buildTree(out,rs.getInt("id"),level);//递归调用
out.println("</div>");
} else
out.print("<img src=\"minus.gif\"> <img src=\"openfold.gif\"> <span onclick=\"myClick1('"+rs.getInt("id")+"');\" style=\"cursor:default;\" id=\"span"+rs.getInt("id")+"\">"+rs.getString("message")+" id="+rs.getInt("id")+"</span>");
out.println("</div>");
}
rs.close();
rs=null;
}

private boolean has_child(int parentid) throws Exception {
ResultSet rs=conn.executeQuery("select * from tree where parentid="+parentid+" order by id");
return rs.next();
}

public String getOption() throws Exception {
String option="";
ResultSet rs=conn.executeQuery("select * from tree order by id");
while(rs.next())
option+="<option value=\""+rs.getInt("id")+"\">"+rs.getInt("id")+"</option> ";
return option;
}
}
%>
<!--以上代码可以写成javabean-->
<script language="JavaScript"><!--这段js为了实现树的展开和关闭的效果-->
<!--
function myClick(id) {
eval("var div=div"+id);
eval("var img=img"+id);
eval("var im=im"+id);
div.style.display=div.style.display!="none"?"none":"block";
img.src=div.style.display!="none"?"minus.gif":"plus.gif";
im.src=div.style.display!="none"?"openfold.gif":"closedfold.gif";
img.alt=div.style.display!="none"?"关闭":"展开";
}
function myClick1(id) {
document.form1.parentid.value=id;
}
//-->
</script>
<table>
<tr><td height="300" valign="top">
<%
tree myTree=new tree();
myTree.init(out,request);
%>
</td></tr>
<tr><td valign="top">
<%
if(myTree.getOption().equals("")) {
%>
<form name="form1" action="" method="get">
parentid:0-表示根节点
message:<input type="text" name="message">
<input type="hidden" name="parentid" value="0">
<input type="submit" name="action" value="add">
</form>
<%} else {%>
<form name="form1" action="" method="get">
parentid:<select name="parentid"><%=myTree.getOption()%></select>
message:<input type="text" name="message">
<input type="submit" name="action" value="add"> <input type="submit" name="action" value="delete">
</form>
<%}%>
</td></tr></table>


嘿嘿结帐吧
回复
BillyW 2004-09-09
能发给我学习一下吗?
billywxy2@hotmail.com
回复
qijiahao 2004-09-09
qijiahao@sohu.com
能否一个呢????
回复
clei1005 2004-09-09
给否给我一份呢?
clei7810@hotmail.com
回复
dropship 2004-09-09
阿信的动态树,就是无限级的。
不过他是asp的,我改了一个jsp的,你有兴趣可以研究一下,不是很难
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-09-08 02:09
社区公告
暂无公告