Ajax+asp unlimited classification tree structure with database, good stuff, don’t miss it, IE test passed, FF has a little bug
Cls_Leibie.asp
Copy the code code as follows:
<%
'Database fields are class attributes, and functions such as adding, deleting, modifying, and operation checking are class methods.
Class Cls_Leibie
Private nClassID,sClassName,nParentID,sParentPath,nDepth,nRootID,nChild,nOrderID,sFilePath 'Define private variables (attributes of the class, that is, variables corresponding to database fields)
Private rs,sql,ErrorStr
Private Sub Class_Initialize()
ErrorStr= 'Initialization error message is empty
End Sub
Private Sub Class_Terminate() 'Close the database connection when destroying the class
If IsObject(Conn) Then
Conn.Close
SetConn=Nothing
End If
End Sub
'************************Set various properties****************************** ***************************
Public Property Let ClassID(str) 'Get the category ID (primary key)
nClassID=str
call ClassProperty() 'Call this function to read out all properties of the class when obtaining the category ID
End Property
Public Property Let ClassName(str) 'Get the class name
sClassName=str
End Property
Public Property Get ClassName
ClassName=sClassName
End Property
Public Property Let ParentID(str) 'Get the category parent ID
nParentID=str
End Property
Public Property Get ParentID
ParentID=nParentID
End Property
Public Property Let ParentPath(str) 'Get the parent path ID
sParentPath=str
End Property
Public Property Get ParentPath
ParentPath=sParentPath
End Property
Public Property Let Depth(str) 'Get category depth
nDepth=str
End Property
Public Property Get Depth
Depth=nDepth
End Property
Public Property Let RootID(str) 'Get the category root ID
nRootID=str
End Property
Public Property Get RootID
RootID=nRootID
End Property
Public Property Let Child(str) 'Number of subcategories
nChild=str
End Property
Public Property Get Child
Child=nChild
End Property
Public Property Let OrderID(str) 'Order ID
nOrderID=str
End Property
Public Property Get OrderID
OrderID=nOrderID
End Property
Public Property Let FilePath(str) 'Category file root directory (generate static file path, Xiaozhan Laoyang Web Technology Blog uses static file generation, so set this field)
sFilePath=str
End Property
Public Property Get FilePath
FilePath=sFilePath
End Property
'************************************************ *******************************
Private Sub ClassProperty() 'Read all properties of the class
sql=select * from ArticleClass where ClassID=& nClassID
set rs=conn.execute(sql)
if not rs.eof then
sClassName=trim(rs(ClassName))
nParentID=trim(rs(ParentID))
sParentPath=trim(rs(ParentPath))
nDepth=trim(rs(Depth))
nRootID=trim(rs(RootID))
nChild=trim(rs(Child))
nOrderID=trim(rs(OrderID))
sFilePath=trim(rs(FilePath))
end if
set rs=nothing
End Sub
Public Function FAddCheck() 'Add a check function to the category. A result of 0 means the check is passed, and a result of 1 means an error occurs. When an error occurs, exit the function and write the error information to the error variable ErrorStr
dim temprs
FAddCheck=0
if sClassName= then 'The class name is empty
FAddCheck=1
ErrorStr=Class name cannot be empty!
exit Function
else
if nParentID= then 'Parent ID is empty
FAddCheck=1
ErrorStr=Parent id cannot be empty!
exit Function
else
if nParentID<>0 then
set temprs=conn.execute(select ClassID From ArticleClass where ClassID= & nParentID) 'The parent category does not exist
if temprs.eof then
FAddCheck=1
ErrorStr=The category does not exist or has been deleted!
exit Function
else
sql=select ClassID from ArticleClass where ClassName='& sClassName &' and ParentID=& nParentID 'Duplicate class name
set rs=conn.execute(sql)
if not rs.eof then
FAddCheck=1
ErrorStr=Duplicate class name!
exit Function
end if
set rs=nothing
end if
settemprs=nothing
else
sql=select ClassID from ArticleClass where ClassName='& sClassName &' and ParentID=& nParentID 'Duplicate class name
set rs=conn.execute(sql)
if not rs.eof then
FAddCheck=1
ErrorStr=Duplicate class name!
exit Function
end if
set rs=nothing
end if
end if
end if
End Function
Public Sub SAdd()
dim maxClassID,maxRootID
set rs = conn.execute(select Max(ClassID) from ArticleClass) 'Find the largest category id in the current database. If there is no data, set it to 0. The category id to be inserted is the current largest id plus 1
maxClassID=rs(0)
if isnull(maxClassID) then
maxClassID=0
end if
set rs=nothing
nClassID=maxClassID+1
set rs=conn.execute(select max(rootid) From ArticleClass) 'Find the largest root id in the current database. If there is no data, set it to 0. The root id to be inserted is the current largest root id plus 1
maxRootID=rs(0)
if isnull(maxRootID) then
maxRootID=0
end if
nRootID=maxRootID+1
set rs=conn.execute(select RootID,Depth,ParentPath,Child,OrderID From ArticleClass where ClassID= & nParentID) 'Find the corresponding information of the parent category
if not rs.eof then
nRootID=trim(rs(Rootid)) 'The root id is the same as the parent category root id
sParentPath=trim(rs(ParentPath))& , &nParentID
if cint(trim(nParentID))>0 then 'If the parent id is greater than 0, there is a parent category, so the depth of the category to be inserted is added to the depth of the parent category by 1. If the parent id is not greater than 0, the current category to be inserted is the root category. Then the depth is 0
nDepth=cint(trim(rs(Depth)))+1
else
nDepth=0
end if
if cint(trim(rs(Child)))>0 then
dimrsPrevOrderID
'Get the OrderID of the last column at the same level as this column
set rsPrevOrderID=conn.execute(select Max(OrderID) From ArticleClass where ParentID= & ParentID)
prevOrderID=rsPrevOrderID(0)
'Get the maximum OrderID of the sub-column of the same parent column but larger than this column. If it is larger than the previous value, use this value instead.
set rsPrevOrderID=conn.execute(select Max(OrderID) From ArticleClass where ParentPath like ' & ParentPath & ,%')
if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
if not IsNull(rsPrevOrderID(0)) then
if rsPrevOrderID(0)>prevOrderID then
prevOrderID=rsPrevOrderID(0)
end if
end if
end if
set rsPrevOrderID=nothing
end if
nOrderID=prevOrderID+1
else
nOrderID=0
sParentPath=0
nDepth=0
end if
set rs=nothing
nChild=0
sql=insert into ArticleClass (ClassID,ClassName,ParentID,ParentPath,Depth,RootID,Child,OrderID,FilePath) values (& nClassID &,'& sClassName &',& nParentID &,'& sParentPath &',& nDepth &, & nRootID &,& nChild &,& nOrderID &,'& sFilePath &')
conn.execute(sql)
if ParentID>0 then
'Update the number of sub-columns of its parent class
conn.execute(update ArticleClass set child=child+1 where ClassID=& nParentID)
'Update the sorting of this column and the sorting sequence numbers of columns that are greater than this need and under this category
if prevOrderID<> then
conn.execute(update ArticleClass set OrderID=OrderID+1 where rootid= & nRootid & and OrderID>& prevOrderID & and ClassID<>& nClassID)
end if
end if
End Sub
Public Function FEditCheck() 'Category modification check function, the result is 0 means the check has been passed, and 1 means an error has occurred. When an error occurs, exit the function and write the error information to the error variable ErrorStr
dim temprs
FEditCheck=0
if nClassID= then 'Category id is empty
FEditCheck=1
ErrorStr=Category id cannot be empty!
exit Function
else
if sClassName= then 'The class name is empty
FEditCheck=1
ErrorStr=Class name cannot be empty!
exit Function
else
if nParentID<>0 then
set temprs=conn.execute(select ClassID From ArticleClass where ClassID= & nParentID) 'The parent category does not exist
if temprs.eof then
FAddCheck=1
ErrorStr=The category does not exist or has been deleted!
exit Function
else
set rs=conn.execute(select ClassID from ArticleClass where ClassName='& sClassName &' and ClassID<>& nClassID &and ParentID=& nParentID)
if not rs.eof then 'Duplicate class name
FEditCheck=1
ErrorStr=Duplicate class name!
exit Function
end if
set rs=nothing
end if
settemprs=nothing
end if
end if
end if
End Function
Public Sub SEdit() 'Category modification
sql=update ArticleClass set ClassName='& sClassName &',FilePath='& sFilePath &' where ClassID=& nClassID
conn.execute(sql)
End Sub
Public Function FDeleteCheck() 'Category deletion check function, the result is 0 means the check has been passed, and 1 means an error has occurred. When an error occurs, exit the function and write the error information to the error variable ErrorStr
FDeleteCheck=0 'Delete the code here without writing the cascade deletion part of the article. When deleting, you should cascade delete it.
if nClassID= then
FDeleteCheck=1
ErrorStr=The category id to be deleted cannot be empty!
exit Function
else
set rs=conn.execute(select Child from ArticleClass where ClassID=& nClassID)
if rs.bof and rs.eof then
FDeleteCheck=1
ErrorStr=The category does not exist or has been deleted!
exit Function
else
if trim(rs(Child))>0 then
FDeleteCheck=1
ErrorStr=This category contains subcategories, please delete its subcategories before deleting this category!
exit Function
end if
end if
end if
End Function
Public Sub SDelete()
if nDepth>0 then 'Modify the number of children of the parent id
conn.execute(update ArticleClass set child=child-1 where child>0 and ClassID= & nParentID)
end if
sql=delete from ArticleClass where ClassID=& nClassID
conn.execute(sql)
End Sub
Public FunctionFErrStr()
FErrStr=ErrorStr
End Function
End Class
%>
index.asp
<%@LANGUAGE=VBSCRIPT CODEPAGE=65001%>
<%
'Author site: www.guaishi.org
'Email: [email protected]
'QQ:514777880
Session.CodePage=65001
Response.Charset = utf-8
%>
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv=Content-Type content=text/html; charset=utf-8 />
<title></title>
<style type=text/css>
body{margin:0;padding:0;font-size:12px; background-color:#FFFFFF;}
ul{ list-style-type:none; margin:0 0 0 20px; padding:0;}
li{ white-space:nowrap; padding:0;}
.childdiv{ background:url(images/dot.gif);background-repeat:repeat-y;}
span { cursor:pointer;}
</style>
<script type=text/javascript>
var xmlHttp; //Define a global variable
var currentID=1;//Set the currently selected ID. If this ID does not exist, a js error will occur.
//Category display main function
//cid--the layer id of the subcategory
//id --category id
//pid--[+] and [-] icon id
//fid--category icon id
function DivDisplay(cid,id,pid,fid)
{
if (GetId(cid).style.display=='') //Icon display control when subcategory is not displayed
{
GetId(cid).style.display='none';
GetId(pid).src = 'images/closed.gif';
GetId(fid).src = 'images/folder.gif';
}
else //Operations when expanding subcategories
{
GetId(cid).style.display='';
GetId(pid).src = 'images/opened.gif';
GetId(fid).src = 'images/folderopen.gif';
if (GetId(cid).innerHTML==''||GetId(cid).innerHTML=='Submitting data...')
{
GetId(cid).innerHTML='';
ShowChild(cid,id); //Call the display subcategory function
}
}
}
//Same effect as the previous function, only works on the last category
function DivDisplay2(cid,id,pid,fid)
{
if (GetId(cid).style.display=='')
{
GetId(cid).style.display='none';
GetId(pid).src = 'images/lastclosed.gif';
GetId(fid).src = 'images/folder.gif';
}
else
{
GetId(cid).style.display='';
GetId(pid).src = 'images/lastopen.gif';
GetId(fid).src = 'images/folderopen.gif';
if (GetId(cid).innerHTML==''||GetId(cid).innerHTML=='Submitting data...')
{
GetId(cid).innerHTML='';
ShowChild(cid,id);
}
}
}
//Category add function
//id--category id
function ClassAdd(id){
if (GetId(p+id).src.indexOf(last)>0){ //Add operation for the last category
if (!GetId(p+id).onclick){
GetId(p+id).onclick=function (){DivDisplay2(c+id,id,p+id,f+id);}; //Add click events for [+] and [-]
GetId(s+id).ondblclick=function (){DivDisplay2(c+id,id,p+id,f+id);}; //Add a double-click event for the span that displays category text
GetId(p+id).src = 'images/lastopen.gif';
}
}
else{
if (!GetId(p+id).onclick){ //Do not add the last category
GetId(p+id).onclick=function (){DivDisplay(c+id,id,p+id,f+id);};
GetId(s+id).ondblclick=function (){DivDisplay(c+id,id,p+id,f+id);};
GetId(p+id).src = 'images/opened.gif';
}
}
GetId(c+id).style.display='';
ShowChild(c+id,id);
}
//Category modification function
function ClassEdit(id,classname){
GetId(s+id).innerHTML=classname;
}
//Delete function for categories with multiple subcategories
function ClassDel(id){
ShowChild(c+id,id);
CurrentSelect(currentID,id)
BrowseRight(id);
}
//Delete function for categories with only one subcategory
function ClassDel1(id){
if (GetId(p+id).src.indexOf(last)>0){ //When the category is the last category of the current category
GetId(p+id).style.cursor=cursor; //Set the mouse passing style of the icon
GetId(p+id).onclick=function (){}; //Because there is only one subcategory after deletion, there will be no more subcategories, so change the icon click event to an empty function
GetId(s+id).ondblclick=function (){}; //Same as above
GetId(p+id).src = 'images/lastnochild.gif'; //Icon settings
}
else{
GetId(p+id).style.cursor=cursor; //Delete operation of non-last category
GetId(p+id).onclick=function (){};
GetId(s+id).ondblclick=function (){};
GetId(p+id).src = 'images/nofollow2.gif'; //The icon setting here is different from the previous one
}
ShowChild(c+id,id);
CurrentSelect(currentID,id);
BrowseRight(id);
}
//Pass parameters to the right frame
function BrowseRight(id){
CurrentSelect(currentID,id);
top.ContentFrame.location=../ArticleMain.asp?ClassID=+ id;
}
//Function to set the selected status of the category
function CurrentSelect(oldid,newid){
currentID=newid;
document.getElementById(s+oldid).style.backgroundColor=white;
document.getElementById(s+currentID).style.backgroundColor=#C0C0E9;
}
//Create XMLHttpRequest object
function CreateXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
}
else
{
xmlHttp = new XMLHttpRequest();
}
}
//Ajax processing function
//id, layer id
//rid, the id of the data in the table
function ShowChild(cid,id)
{
CreateXMLHttpRequest();
if(xmlHttp)
{
xmlHttp.open('POST','child.asp',true);
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
var SendData = 'id='+id;
xmlHttp.send(SendData);
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
GetId(cid).innerHTML = xmlHttp.responseText;
}
else
{
GetId(cid).innerHTML='Error:'+xmlHttp.statusText;
}
}
else
{
GetId(cid).innerHTML=Submitting data...;
}
}
}
else
{
GetId(cid).innerHTML='Sorry, your browser does not support XMLHttpRequest, please use IE6 or above! ';
}
}
//Get the page object
//id, layer id
function GetId(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<!--#include file=../conn.asp-->
<%
'Show root directory
sql=select *,(select top 1 ClassID from ArticleClass where Depth=0 order by ClassID desc) as lastid from ArticleClass where Depth=0 order by ClassID
set rs=conn.execute(sql)
if not rs.eof then
response.Write <ul>&vbcr
do while not rs.eof
if cint(trim(rs(ClassID)))=cint(trim(rs(lastid))) then
if rs(Child)>0 then
response.Write <li><img id='p&rs(ClassID)&' src=images/lastclosed.gif onclick=DivDisplay2('c&rs(ClassID)&','&rs(ClassID)&','p&rs(ClassID)& ','f& rs(ClassID) &') style=cursor : hand; align=absmiddle>
response.Write <img src=images/folder.gif align=absmiddle id='f& rs(ClassID) &' /> <span id='s& trim(rs(ClassID)) &' onclick=BrowseRight(& trim(rs (ClassID)) &) ondblclick=DivDisplay2('c&rs(ClassID)&','&rs(ClassID)&','p&rs(ClassID)&','f& rs(ClassID) &')>& rs(ClassName) &</span>
else
response.Write <li><img id='p& rs(ClassID) &' src=images/lastnochild.gif align=absmiddle />
response.Write <img src=images/folder.gif align=absmiddle id='f& rs(ClassID) &' /> <span id='s& trim(rs(ClassID)) &' onclick=BrowseRight(& trim(rs (ClassID)) &)>& rs(ClassName) &</span>
end if
else
if rs(Child)>0 then
response.Write <li><img id='p&rs(ClassID)&' src=images/closed.gif onclick=DivDisplay('c&rs(ClassID)&','&rs(ClassID)&','p&rs(ClassID)& ','f& rs(ClassID) &') style=cursor : hand; align=absmiddle>
response.Write <img src=images/folder.gif align=absmiddle id='f& rs(ClassID) &' /> <span id='s& trim(rs(ClassID)) &' onclick=BrowseRight(& trim(rs (ClassID)) &) ondblclick=DivDisplay('c&rs(ClassID)&','&rs(ClassID)&','p&rs(ClassID)&','f& rs(ClassID) &')>& rs(ClassName) &</span>
else
response.Write <li><img id='p& rs(ClassID) &' src=images/nofollow2.gif align=absmiddle />
response.Write <img src=images/folder.gif align=absmiddle id='f& rs(ClassID) &' /> <span id='s& trim(rs(ClassID)) &' onclick=BrowseRight(& trim(rs (ClassID)) &)>& rs(ClassName) &</span>
end if
end if
if cint(trim(rs(ClassID)))=cint(trim(rs(lastid))) then
response.Write <div id='c&rs(ClassID)&' style='display:none;'></div>
else
response.Write <div id='c&rs(ClassID)&' style='display:none;' class=childdiv></div>
end if
response.Write </li>&vbcr
rs.movenext
loop
response.Write </ul>&vbcr
end if
rs.close
set rs=nothing
conn.close
Set conn = Nothing
%>
</body>
</html>