DIV树:
1、servlet里:
/**
* 完成分销商树的递归读取
* @author Administrator
*
*/
public class ClientTreeReader {
private StringBuffer sbTreeHTML = new StringBuffer();
/**
* 返回HTML字符串
* @return
*/
public String getClientTreeHTMLString() {
Connection conn = null;
try {
conn = DbUtil.getConnection();
readClientTree(conn, 0, 0);
}catch(Exception e) {
e.printStackTrace();
}finally {
DbUtil.close(conn);
}
return sbTreeHTML.toString();
}
// /**
// * 递归读取分销商树
// *
// * 第一步: 以最简单的方式读取分销商树形结构,可以显示到浏览器中即可
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// sbTreeHTML.append(rs.getString("name"))
// .append("<br>\n");
// if ("N".equals(rs.getString("is_leaf"))) {
// readClientTree(conn, rs.getInt("id"), level);
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
// /**
// * 递归读取分销商树
// *
// * 第二步: 加入层次感
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// for (int i=0; i<level; i++) {
// sbTreeHTML.append(" ");
// }
// sbTreeHTML.append(rs.getString("name"))
// .append("<br>\n");
// if ("N".equals(rs.getString("is_leaf"))) {
// readClientTree(conn, rs.getInt("id"), level + 1);
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
// /**
// * 递归读取分销商树
// *
// * 第三步: 叶子节点前面加入一个“-”号,非叶子节点前面加入一个“+”号
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// for (int i=0; i<level; i++) {
// sbTreeHTML.append(" ");
// }
// if ("N".equals(rs.getString("is_leaf"))) {
// sbTreeHTML.append("+")
// .append(rs.getString("name"))
// .append("<br>\n");
// readClientTree(conn, rs.getInt("id"), level + 1);
// }else {
// sbTreeHTML.append("-")
// .append(rs.getString("name"))
// .append("<br>\n");
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
/**
* 递归读取分销商树
*
* 第四步: 采用div生成树形结构
* @param conn
* @param id
* @param level 控制层次
*/
private void readClientTree(Connection conn, int id, int level)
throws SQLException {
String sql = "select * from t_client where pid=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
sbTreeHTML.append("<div>");
sbTreeHTML.append("\n");
for (int i=0; i<level; i++) {
sbTreeHTML.append("<img src=\"../images/white.gif\">");
sbTreeHTML.append("\n");
}
//if ("N".equals(rs.getString("is_leaf"))) {
if (Constants.NO.equals(rs.getString("is_leaf"))) {
sbTreeHTML.append("<img alt=\"展开\" style=\"cursor:hand;\" onClick=\"display('" + rs.getInt("id") + "');\" id=\"img" + rs.getInt("id") + "\" src=\"../images/plus.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<img id=\"im" + rs.getInt("id") + "\" src=\"../images/closedfold.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<a href=\"client_node_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
sbTreeHTML.append("\n");
sbTreeHTML.append("<div style=\"display:none;\" id=\"div" + rs.getInt("id") + "\">");
sbTreeHTML.append("\n");
readClientTree(conn, rs.getInt("id"), level + 1);
sbTreeHTML.append("</div>");
sbTreeHTML.append("\n");
}else {
sbTreeHTML.append("<img src=\"../images/minus.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<img src=\"../images/openfold.gif\">");
sbTreeHTML.append("\n");
//if ("Y1".equals(rs.getString("is_client"))) {
if (Constants.YES.equals(rs.getString("is_client"))) {
sbTreeHTML.append("<a href=\"client_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
}else {
sbTreeHTML.append("<a href=\"client_node_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
}
sbTreeHTML.append("\n");
}
sbTreeHTML.append("</div>");
sbTreeHTML.append("\n");
}
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
}
2、jsp里:
<script language="JavaScript">
<!--
function display(id) {
eval("var div=div"+id);
eval("var img=img"+id);
eval("var im=im"+id);
div.style.display=div.style.display=="block"?"none":"block";
img.src=div.style.display=="block"?"../images/minus.gif":"../images/plus.gif";
im.src=div.style.display=="block"?"../images/openfold.gif":"../images/closedfold.gif";
img.alt=div.style.display=="block"?"关闭":"展开";
}
//-->
</script>
</head>
<body class="body1">
<table>
<tr>
<td valign="top" nowrap="nowrap">
<%=ClientManager.getInstance().getClientTreeHTMLString() %>
</td>
</tr>
</table>
</body>
1、servlet里:
/**
* 完成分销商树的递归读取
* @author Administrator
*
*/
public class ClientTreeReader {
private StringBuffer sbTreeHTML = new StringBuffer();
/**
* 返回HTML字符串
* @return
*/
public String getClientTreeHTMLString() {
Connection conn = null;
try {
conn = DbUtil.getConnection();
readClientTree(conn, 0, 0);
}catch(Exception e) {
e.printStackTrace();
}finally {
DbUtil.close(conn);
}
return sbTreeHTML.toString();
}
// /**
// * 递归读取分销商树
// *
// * 第一步: 以最简单的方式读取分销商树形结构,可以显示到浏览器中即可
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// sbTreeHTML.append(rs.getString("name"))
// .append("<br>\n");
// if ("N".equals(rs.getString("is_leaf"))) {
// readClientTree(conn, rs.getInt("id"), level);
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
// /**
// * 递归读取分销商树
// *
// * 第二步: 加入层次感
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// for (int i=0; i<level; i++) {
// sbTreeHTML.append(" ");
// }
// sbTreeHTML.append(rs.getString("name"))
// .append("<br>\n");
// if ("N".equals(rs.getString("is_leaf"))) {
// readClientTree(conn, rs.getInt("id"), level + 1);
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
// /**
// * 递归读取分销商树
// *
// * 第三步: 叶子节点前面加入一个“-”号,非叶子节点前面加入一个“+”号
// * @param conn
// * @param id
// * @param level 控制层次
// */
// private void readClientTree(Connection conn, int id, int level)
// throws SQLException {
// String sql = "select * from t_client where pid=?";
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = conn.prepareStatement(sql);
// pstmt.setInt(1, id);
// rs = pstmt.executeQuery();
// while (rs.next()) {
// for (int i=0; i<level; i++) {
// sbTreeHTML.append(" ");
// }
// if ("N".equals(rs.getString("is_leaf"))) {
// sbTreeHTML.append("+")
// .append(rs.getString("name"))
// .append("<br>\n");
// readClientTree(conn, rs.getInt("id"), level + 1);
// }else {
// sbTreeHTML.append("-")
// .append(rs.getString("name"))
// .append("<br>\n");
// }
// }
// }finally {
// DbUtil.close(rs);
// DbUtil.close(pstmt);
// }
// }
/**
* 递归读取分销商树
*
* 第四步: 采用div生成树形结构
* @param conn
* @param id
* @param level 控制层次
*/
private void readClientTree(Connection conn, int id, int level)
throws SQLException {
String sql = "select * from t_client where pid=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
sbTreeHTML.append("<div>");
sbTreeHTML.append("\n");
for (int i=0; i<level; i++) {
sbTreeHTML.append("<img src=\"../images/white.gif\">");
sbTreeHTML.append("\n");
}
//if ("N".equals(rs.getString("is_leaf"))) {
if (Constants.NO.equals(rs.getString("is_leaf"))) {
sbTreeHTML.append("<img alt=\"展开\" style=\"cursor:hand;\" onClick=\"display('" + rs.getInt("id") + "');\" id=\"img" + rs.getInt("id") + "\" src=\"../images/plus.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<img id=\"im" + rs.getInt("id") + "\" src=\"../images/closedfold.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<a href=\"client_node_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
sbTreeHTML.append("\n");
sbTreeHTML.append("<div style=\"display:none;\" id=\"div" + rs.getInt("id") + "\">");
sbTreeHTML.append("\n");
readClientTree(conn, rs.getInt("id"), level + 1);
sbTreeHTML.append("</div>");
sbTreeHTML.append("\n");
}else {
sbTreeHTML.append("<img src=\"../images/minus.gif\">");
sbTreeHTML.append("\n");
sbTreeHTML.append("<img src=\"../images/openfold.gif\">");
sbTreeHTML.append("\n");
//if ("Y1".equals(rs.getString("is_client"))) {
if (Constants.YES.equals(rs.getString("is_client"))) {
sbTreeHTML.append("<a href=\"client_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
}else {
sbTreeHTML.append("<a href=\"client_node_crud.jsp?id=" + rs.getInt("id") + "\" target=\"clientDispAreaFrame\">" + rs.getString("name") + "</a>");
}
sbTreeHTML.append("\n");
}
sbTreeHTML.append("</div>");
sbTreeHTML.append("\n");
}
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
}
2、jsp里:
<script language="JavaScript">
<!--
function display(id) {
eval("var div=div"+id);
eval("var img=img"+id);
eval("var im=im"+id);
div.style.display=div.style.display=="block"?"none":"block";
img.src=div.style.display=="block"?"../images/minus.gif":"../images/plus.gif";
im.src=div.style.display=="block"?"../images/openfold.gif":"../images/closedfold.gif";
img.alt=div.style.display=="block"?"关闭":"展开";
}
//-->
</script>
</head>
<body class="body1">
<table>
<tr>
<td valign="top" nowrap="nowrap">
<%=ClientManager.getInstance().getClientTreeHTMLString() %>
</td>
</tr>
</table>
</body>
</html>
参照:client_tree.jsp与ClientTreeReader.java