简单的二级树
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Sql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("check.mdb")&";User Id=admin;Password=;"
Conn.Open Sql
set rs = server.CreateObject("adodb.recordset")
set rs1 = server.CreateObject("adodb.recordset")
sql = "select * from tree where parentid = 0"
s_sql = "select * from tree where parentid <> 0"
rs.open sql,conn,1,3
rs1.open s_sql,conn,1,3
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="JavaScript">
function show(id){
var c = document.all('child'+id);
var r = document.all('root'+id);
if (c.style.display == 'block'){
c.style.display = 'none';
r.innerHTML = '+';
}
else{
c.style.display = 'block';
r.innerHTML = '-';
}
}
</script>
<body>
<table width="17%" border="0" cellspacing="0" cellpadding="0">
<%
while Not rs.eof%>
<tr style="cursor:hand;">
<td width="11%" align="center">
<div id="root<%=trim(rs("id"))%>" style="cursor:hand;" onClick="show('<%=trim(rs("id"))%>');">+</div></td>
<td width="89%" onClick="show('<%=trim(rs("id"))%>');"><%=trim(rs("parent"))%></td>
</tr>
<tr>
<td colspan="2"> <div id="child<%=trim(rs("id"))%>" style="display:none;cursor:hand;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<% rs1.movefirst
while Not rs1.eof
if trim(rs1("parentid")) = trim(rs("childid")) then%>
<tr>
<td width="10%" align="center"> └</td>
<td width="90%" style="cursor:hand;"><%=trim(rs1("parent"))%></td>
</tr>
<% end if
rs1.movenext
Wend
%>
</table> </div>
</td>
</tr>
<%
rs.movenext
Wend
rs1.close
rs.close%>
</table>
<table width="26%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
<%
conn.close
set conn = nothing
%>
下面是表结构:
create table tree(
partnerid int,
childid int,
name char(20)
)
insert into tree values(0,1,'父A')
insert into tree values(0,2,'父B')
insert into tree values(0,3,'父C')
insert into tree values(1,0,'子A1')
insert into tree values(1,0,'子A2')
insert into tree values(2,0,'子B1')
insert into tree values(2,0,'子B2')
insert into tree values(3,0,'子C1')
insert into tree values(3,0,'子C2')