package com.netmarch.folder;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Tree {
Cn conn = new Cn();
//注意这里的文件中心对应的ID号为491,如果您对应的不是这个ID,请您自己修改一下ID号
public void init(javax.servlet.jsp.JspWriter out,
javax.servlet.http.HttpServletRequest request) throws Exception {
out.println("<title>文件中心</title>");
dowith(request);
buildTree(out, 491, 0, "0");// 初始调用
}
//下面代码为测试类。
private void dowith(javax.servlet.http.HttpServletRequest request)
throws SQLException {
if (request.getParameter("parentid") == null
|| request.getParameter("parentid").equals(""))
return;
String action = request.getParameter("action");
if (action.equals("add"))
conn.executeUpdate("insert into item(parentid,name) values('"
+ request.getParameter("parentid") + "','"
+ request.getParameter("name") + "')");
else if (action.equals("delete"))
conn.executeUpdate("delete from item where id="
+ request.getParameter("parentid") + " or parentid="
+ request.getParameter("parentid"));
}
public void buildTree(javax.servlet.jsp.JspWriter out, int parentid,
int level, String pids) throws Exception {
level++;
ResultSet rs = conn
.executeQuery("select * from item where sn like '491,%' and webid=97 and isAuditing=1 and parentid="
+ parentid + " order by id");
String pic="";
///ResultSet rs = conn.executeQuery("select a.id,a.parentid,b.id,b.parentid,a.name,b.name from item a ,item b where ( a.id =b.parentid ) and a.id =491 ");
while (rs.next()) {
out.println("<div>");
for (int i = 0; i < level - 1; i++)
out.print("<img src=\"img/T.gif\"> ");
//if (has_child(rs.getInt("id"))) {
if(level==1){
pic="plus.gif";
}else{
pic="arrow.gif";
}
out
.print("<img alt=\"展开\" style=\"cursor:hand;\" onclick=\"myClick('"
+ rs.getInt("id")
+ "');\" id=\"img"
+ rs.getInt("id")
+ "\" src=\"img/"+pic+"\">");
out.print("<span onclick=\"myClick1('" + rs.getInt("id")
+ "');\" style=\"cursor:default;\" id=\"span"
+ rs.getInt("id") + "\">" + rs.getString("name")
+ "</span>");
out.println("<div style=\"display:none;\" id=\"div"
+ rs.getInt("id") + "\" pa='" + pids + ","
+ rs.getInt("id") + "'>");
buildTree(out, rs.getInt("id"), level, pids + ","
+ rs.getInt("id"));//递归调用
out.println("</div>");
//} else
// out
// .print("<img src=\"img/minus.gif\"> <span onclick=\"myClick1('"
// + rs.getInt("id")
// + "');\" style=\"cursor:default;\" id=\"span"
// + rs.getInt("id")
// + "\">"
// + rs.getString("name")
// + "</span>");
out.println("</div>");
}
rs.close();
rs = null;
}
private boolean has_child(int parentid) throws Exception {
ResultSet rs = conn
.executeQuery("select * from item where sn like '491,%' and webid=97 and isAuditing=1 and parentid="
+ parentid + " order by id");
return rs.next();
}
//注意。这里的like '491,%'是妈出最大的级数,呵呵,并且你要在你的表里面建一个SN,例子如下:491,表示为第一级以此类推
//下面这个是测试用
public String getOption() throws Exception {
String option = "";
ResultSet rs = conn.executeQuery("select id from item order by id");
while (rs.next())
option += "<option value=\"" + rs.getInt("id") + "\">"
+ rs.getInt("id") + "</option>\n";
return option;
}
}