ASP + XML + JavaScript to implement dynamic Infinite-level linkage menu

Source: Internet
Author: User
xml| Menu | dynamic | linkage Menu |xml| Menu | Linkage menu When I do a project, there is a need to do a dynamic unlimited level linkage menu. Because I am lazy so the Internet to find relevant code, but many did not meet the demand, which has a article is the use of JavaScript to XML file operation to achieve an unlimited level linkage menu, we can combine ASP to complete the reading of database values, and then write XML file, Then read it out with JavaScript and control its linkage.
The key here is to read the N-tier data classes in the database:
The structure of my database table is this:
' Tbl_class
Column name data type length description
ClassID int 4 class ID
ModuleID int 4 Module ID
GroupID int 2 identifies a group
ClassName nvarchar 50 category name
ParentID int 2 is connected to a group (0 is the parent class)


' ################################### #我的ASP代码如下 ##########################################
' I ignored the code to connect to the database.


' Function name: OpenXml (FileName)
' Entry parameter: FileName required to be connected or opened by XML filename
' Return value: XmlDoc is an object that successfully loads an XML document.
' Error printing error message strerror
'------------------------------------------------

function OpenXml (filename)
Dim strsourcefile, Xmldoc,strerror
strSourceFile = filename
Set xmldoc = Server.CreateObject ("microsoft.xmldom") ' Create xmldom instance
Xmldoc.async = False
Xmldoc.load (strSourceFile)
Openxml=xmldoc.parseerror.errorcode
If Xmldoc.parseerror.errorcode<>0 Then
Strerror= "strerror=strerror&xmldoc.parseerror.reason& "<br>"
strerror=strerror&xmldoc.parseerror.url& "<br>"
strerror=strerror&xmldoc.parseerror.line& "<br>"
strerror=strerror&xmldoc.parseerror.filepos& "<br>"
strerror=strerror&xmldoc.parseerror.srctext& "<br>"
Response.Write strerror ' output error
Else
Set Openxml=xmldoc ' Returns an instance
End If
End Function


'------------------------------------------------
' Function name: Closexml ()
' Parameter: XmlDoc XML component Instance
'------------------------------------------------
function Closexml (xmldoc)
If IsObject (xmldoc) Then
Set xmldoc=nothing
End If
End Function


'------------------------------------------------
' Function name: Selectxmlnode
' Parameter: XmlDoc XML component Instance
' e element's name
' Returns an element instance
'------------------------------------------------
function Selectxmlnode (xmldoc,e)
Dim n

Set N=xmldoc.selectsinglenode ("//" & E)
Set selectxmlnode= n

End Function


Dim N,np,maxgroup,root,xmldoc,nt,filename,s,ss,torf
Filename=server.mappath ("Demo.xml")
Set xmldoc=openxml (filename) ' opens XML
Removeallnodes xmldoc, "root" clears root and the subkeys below it, so that it can be easily read and written
Set root= xmldoc.createelement ("root")
Xmldoc.appendchild Root ' creates a top-level element
Sql= "select Max (GroupID) from Tbl_class" read the largest level
Set Rs=cn.execute (SQL)
If IsNull (RS (0)) then Maxgroup=0 else maxgroup=rs (0) ' If NULL indicates no data
S= "": ss= ""
Set Nt=xmldoc.createelement ("item")
Nt.setattribute "text", "Please select"
Root.appendchild nt ' Create an element
For I=1 to Maxgroup ' Start loop
Sql= "SELECT * from Tbl_class where groupid=" & I "read from bottom to top level
Set Rs=cn.execute (SQL)
Torf=false ' Create a "Please select" Null for each layer.
Do While rs.eof =false ' starts reading the underlying data
Set n = xmldoc.createelement ("Item" & RS ("ClassID")) ' Create a markup element named Item + ID number
N.setattribute "Text", RS ("ClassName") ' sets its property ' text ' to the database table

ClassName
N.setattribute "Value", RS ("ClassID") ' sets its property ' value ' to the database table

ClassID
If RS ("ParentID") >0 then ' is an upper-level data
Set Np=selectxmlnode (xmldoc, "Item" & RS ("ParentID")) ' reads its upper-layer data element first and saves it in NP
If Torf=false then ' if Torf is a false value
Set Nt=xmldoc.createelement ("item") ' Creates an element to save in NT
Nt.setattribute "Text", "select" To set its Text property to "please select"
Np.appendchild NT ' NP adds it as a child element
End If
Torf=true ' Set True
Np.appendchild N ' NP adds n as a child element
Else
Root.appendchild N ' If it's the first level of data, add it as a child element under root
End If
Rs.movenext ' next pointer
Loop

Ss=ss & "<select id=select" & I & "Width=1 ></SELECT></span>" ' Create one on each level

<select>
S=s & ", ' Select ' & I & '" ' Saves the ID of each <select> in the variable s, which is in the format: ID1,ID2,ID3,ID4

Next
Xmldoc.save filename ' official save XML file
Closexml xmldoc ' Close XML file


Response.Write SS ' Direct <select> output to document
S=mid (s,2)




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.