Ajax + asp unlimited classification tree structure (with database)

Source: Internet
Author: User

IE passed the test. FF has a small BUG.
Cls_Leibie.asp Copy codeThe Code is as follows: <%
'Database fields are class attributes, and functions such as adding, deleting, modifying, and checking operations are class methods.
Class Cls_Leibie
Private nClassID, sClassName, nParentID, sParentPath, nDepth, nRootID, nChild, nOrderID, sfilepath' defines Private variables (class attributes, that is, the variables corresponding to database fields)
Private rs, SQL, ErrorStr
Private Sub Class_Initialize ()
ErrorStr = "" 'initialization error message is blank
End Sub
Private Sub Class_Terminate () 'Close the database connection when the class is destroyed
If IsObject (Conn) Then
Conn. Close
Set Conn = Nothing
End If
End Sub
***************** *************************************
Public Property Let ClassID (str) 'Get category ID (primary key)
NClassID = str
Call ClassProperty () 'to obtain the category ID. This function is called to read all attributes of the class.
End Property
Public Property Let ClassName (str) 'Get the category name
SClassName = str
End Property
Public Property Get ClassName
ClassName = sClassName
End Property
Public Property Let ParentID (str) 'Get 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 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) 'sorting 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. This field is set for the website's old Yang Web technology blog to generate static files)
SFilePath = str
End Property
Public Property Get FilePath
FilePath = sFilePath
End Property
'*************************************** ***************************************
Private Sub ClassProperty () 'reads all attributes of the class
SQL = "select * from ArticleClass where ClassID =" & nClassID
Set rsw.conn.exe cute (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 () 'class adds a check Function. If the result is 0, the system passes the check. If the result is 1, an error occurs. If an error occurs, the system exits the Function and writes the error information to the error variable ErrorStr.
Dim temprs
FAddCheck = 0
If sClassName = "" then "class name is empty
FAddCheck = 1
ErrorStr = "the class name cannot be blank! "
Exit Function
Else
If nParentID = "" then' parent id is null
FAddCheck = 1
ErrorStr = "parent id cannot be blank! "
Exit Function
Else
If nParentID <> 0 then
Set temprs1_conn.exe cute ("select ClassID From ArticleClass where ClassID =" & nParentID) '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 names
Set rsw.conn.exe cute (SQL)
If not rs. eof then
FAddCheck = 1
ErrorStr = "Duplicate class name! "
Exit Function
End if
Set rs = nothing
End if
Set temprs = nothing
Else
SQL = "select ClassID from ArticleClass where ClassName = '" & sClassName & "' and ParentID =" & nParentID 'duplicate class names
Set rsw.conn.exe cute (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.exe cute ("select Max (ClassID) from ArticleClass") 'searches for the largest category id in the current database. If no data exists, set it to 0, the category id to be inserted is the current maximum id plus 1
MaxClassID = rs (0)
If isnull (maxClassID) then
MaxClassID = 0
End if
Set rs = nothing
NClassID = maxClassID + 1
Set rs1_conn.exe cute ("select max (rootid) From ArticleClass") 'to find the largest root id in the current database. If no data exists, set it to 0, the root id to be inserted is the current maximum root id plus 1
MaxRootID = rs (0)
If isnull (maxRootID) then
MaxRootID = 0
End if
NRootID = maxRootID + 1
Set rs1_conn.exe cute ("select RootID, Depth, ParentPath, Child, OrderID From ArticleClass where ClassID =" & nParentID) 'find information about the parent category
If not rs. eof then
NRootID = trim (rs ("Rootid") 'root id is the same as the root id of the parent category
SParentPath = trim (rs ("ParentPath") & "," & nParentID
If cint (trim (nParentID)> 0 then' if the parent id is greater than 0, a parent category exists. Therefore, the depth of the parent category to be inserted is added to 1, if the parent id is not greater than 0, the current category to be inserted is the root category, and the depth is 0.
NDepth = cint (trim (rs ("Depth") + 1
Else
NDepth = 0
End if
If cint (trim (rs ("Child")> 0 then
Dim rsPrevOrderID
'Get the OrderID of the last topic at the same level as this topic.
Set rsPrevOrderID=conn.exe cute ("select Max (OrderID) From ArticleClass where ParentID =" & ParentID)
PrevOrderID = rsPrevOrderID (0)
'Obtain the maximum OrderID of the subitem with the same parent column but greater than the level of this column. If it is greater than the previous value, use this value.
Set rsPrevOrderID=conn.exe cute ("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.exe cute (SQL)
If ParentID> 0 then
'Update the number of subcolumns of the parent class
Conn.exe cute ("update ArticleClass set child = child + 1 where ClassID =" & nParentID)
'Update the sorting of this topic and the sequence number of the topic that exceeds this requirement and under this category.
If prevOrderID <> "then
Conn.exe cute ("update ArticleClass set OrderID = OrderID + 1 where rootid =" & nRootid & "and OrderID>" & prevOrderID & "and ClassID <>" & nClassID)
End if
End if
End Sub
Public Function FEditCheck () 'class modification check Function. If the result is 0, the system passes the check. If the result is 1, an error occurs. If an error occurs, the system exits the Function and writes the error information to the error variable ErrorStr.
Dim temprs
FEditCheck = 0
If nClassID = "" then' category id is null
FEditCheck = 1
ErrorStr = "category id cannot be blank! "
Exit Function
Else
If sClassName = "" then "class name is empty
FEditCheck = 1
ErrorStr = "the class name cannot be blank! "
Exit Function
Else
If nParentID <> 0 then
Set temprs1_conn.exe cute ("select ClassID From ArticleClass where ClassID =" & nParentID) '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 rs1_conn.exe cute ("select ClassID from ArticleClass where ClassName = '" & sClassName & "' and ClassID <>" & nClassID & "and ParentID =" & nParentID)
If not rs. eof then' class name already exists
FEditCheck = 1
ErrorStr = "Duplicate class name! "
Exit Function
End if
Set rs = nothing
End if
Set temprs = 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.exe cute (SQL)
End Sub
Public Function FDeleteCheck () 'class Delete check Function. If the result is 0, the Function passes the check. If the result is 1, an error occurs. If an error occurs, exit the Function and write the error information to the error variable ErrorStr.
FDeleteCheck = 0' Delete the code that does not write cascade deletion of the article part. Delete the code in cascade mode.
If nClassID = "" then
FDeleteCheck = 1
ErrorStr = "the category id to be deleted cannot be blank! "
Exit Function
Else
Set rs1_conn.exe cute ("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 this category before deleting it! "
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.exe cute ("update ArticleClass set child = child-1 where child> 0 and ClassID =" & nParentID)
End if
SQL = "delete from ArticleClass where ClassID =" & nClassID
Conn.exe cute (SQL)
End Sub
Public Function FErrStr ()
FErrStr = ErrorStr
End Function
End Class
%>
Index. asp
<% @ LANGUAGE = "VBSCRIPT" CODEPAGE = "65001" %>
<%
'Author site: www.guaishi.org
'Mail: guaishiorg@126.com
'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; // defines 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 -- 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 = '') // when the subcategory is not displayed, the icon is displayed.
{
GetId (cid). style. display = 'none ';
GetId (pid). src = 'images/closed.gif ';
GetId (fid). src = 'images/folder.gif ';
}
Else // actions for expanding sub-categories
{
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 to display subcategory Functions
}
}
}
// Same as the previous function, it only applies to 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 );
}
}
}
// Add a function to a category
// 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 a click event for [+] and [-]
GetId ("s" + id ). ondblclick = function () {DivDisplay2 ("c" + id, id, "p" + id, "f" + id );}; // Add a double-click event for the span of the displayed category text
GetId ("p" + id). src = 'images/lastopen.gif ';
}
}
Else {
If (! GetId ("p" + id). onclick) {// not add operation for 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 functions with multiple subcategories
Function ClassDel (id ){
ShowChild ("c" + id, id );
CurrentSelect (currentID, id)
BrowseRight (id );
}
// Delete a function with only one subclass
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 style of the icon
GetId ("p" + id). onclick = function () {}; // because only one subcategory exists after it is deleted, the icon click event is changed to an empty function.
GetId ("s" + id). ondblclick = function () {}; // same as above
GetId ("p" + id). src = 'images/lastnochild.gif '; // set the icon
}
Else {
GetId ("p" + id). style. cursor = "cursor"; // delete a non-last category
GetId ("p" + id). onclick = function (){};
GetId ("s" + id). ondblclick = function (){};
GetId ("p" + id). src = 'images/nofollow2.gif '; // The icon settings here are different from those above
}
ShowChild ("c" + id, id );
CurrentSelect (currentID, id );
BrowseRight (id );
}
// Pass parameters to the framework on the right
Function BrowseRight (id ){
CurrentSelect (currentID, id );
Top. ContentFrame. location = ".../ArticleMain. asp? ClassID = "+ id;
}
// Set the function in 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 an XMLHttpRequest object
Function CreateXMLHttpRequest ()
{
If (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else
{
XmlHttp = new XMLHttpRequest ();
}
}
// Ajax processing functions
// 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 later! ';
}
}
// Get the Page Object
// Id, layer id
Function GetId (id)
{
Return document. getElementById (id );
}
</Script>
</Head>
<Body>
<! -- # Include file = "../conn. asp" -->
<%
'Display 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 rsw.conn.exe cute (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> Response. write " <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> Response. write " <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> Response. write " <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> Response. write " <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>

Packaging http://xiazai.jb51.net/200906/yuanma/ajaxtree_51281.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.