ajax+asp怎么實(shí)現(xiàn)無限級分類樹型結(jié)構(gòu)

本篇內(nèi)容主要講解“ajax+asp怎么實(shí)現(xiàn)無限級分類樹型結(jié)構(gòu)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“ajax+asp怎么實(shí)現(xiàn)無限級分類樹型結(jié)構(gòu)”吧!

我們擁有10年網(wǎng)頁設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、移動網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。

復(fù)制代碼 代碼如下:


<%
'數(shù)據(jù)庫字段為類屬性,添加、刪除、修改、操作檢查等函數(shù)為類的方法
Class Cls_Leibie
    Private nClassID,sClassName,nParentID,sParentPath,nDepth,nRootID,nChild,nOrderID,sFilePath '定義私有變量(類的屬性,即數(shù)據(jù)庫字段對應(yīng)的變量)
    Private rs,sql,ErrorStr

    Private Sub Class_Initialize()
        ErrorStr=""                    '初始化錯誤信息為空
    End Sub

    Private Sub Class_Terminate()    '銷毀類時關(guān)閉數(shù)據(jù)庫連接
        If IsObject(Conn) Then 
            Conn.Close
            Set Conn = Nothing
        End If
    End Sub

'*******************設(shè)置各個屬性******************************************************    
    Public Property Let ClassID(str)    '獲取類別ID(主鍵)
        nClassID=str
        call ClassProperty()            '獲取類別ID時調(diào)用此函數(shù)讀出類的所有屬性
    End Property
    Public Property Let ClassName(str)    '獲取類別名稱
        sClassName=str
    End Property

    Public Property Get ClassName
        ClassName=sClassName
    End Property

    Public Property Let ParentID(str)    '獲取類別父ID
        nParentID=str
    End Property

    Public Property Get ParentID
        ParentID=nParentID
    End Property

    Public Property Let ParentPath(str)    '獲取父路徑ID
        sParentPath=str
    End Property

    Public Property Get ParentPath
        ParentPath=sParentPath
    End Property

    Public Property Let Depth(str)        '獲取類別深度
        nDepth=str
    End Property

    Public Property Get Depth
        Depth=nDepth
    End Property

    Public Property Let RootID(str)        '獲取類別根ID
        nRootID=str
    End Property

    Public Property Get RootID
        RootID=nRootID
    End Property

    Public Property Let Child(str)        '子類別個數(shù)
        nChild=str
    End Property

    Public Property Get Child
        Child=nChild
    End Property

    Public Property Let OrderID(str)    '排序ID
        nOrderID=str
    End Property

    Public Property Get OrderID
        OrderID=nOrderID
    End Property
    Public Property Let FilePath(str)    '類別文件根目錄(生成靜態(tài)文件路徑,小站奇人異事網(wǎng)([url]www.guaishi.org[/url])用的是生成靜態(tài),故設(shè)置此字段)
        sFilePath=str
    End Property

    Public Property Get FilePath
        FilePath=sFilePath
    End Property    
'******************************************************************************

    Private Sub ClassProperty()            '讀取類的所有屬性
        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()        '類別添加檢查函數(shù),結(jié)果為0表示通過檢查,為1表示有錯誤發(fā)生,有錯誤發(fā)生時退出函數(shù),將錯誤信息寫入錯誤變量ErrorStr
        dim temprs
        FAddCheck=0
        if sClassName="" then        '類名為空
            FAddCheck=1
            ErrorStr="類名不能為空!"
            exit Function
        else            
            if nParentID="" then        '父id為空
                FAddCheck=1
                ErrorStr="父id不能為空!"
                exit Function
            else
                if nParentID<>0 then
                    set temprs=conn.execute("select ClassID From ArticleClass where ClassID=" & nParentID)        '父類別不存在
                    if temprs.eof then
                        FAddCheck=1
                        ErrorStr="所屬類別不存在或已經(jīng)被刪除!"
                        exit Function
                    else
                        sql="select ClassID from ArticleClass where ClassName='"& sClassName &"' and ParentID="& nParentID        '類名重復(fù)
                        set rs=conn.execute(sql)
                        if not rs.eof then
                            FAddCheck=1
                            ErrorStr="類名重復(fù)!"
                            exit Function
                        end if
                        set rs=nothing
                    end if
                    set temprs=nothing
                else
                    sql="select ClassID from ArticleClass where ClassName='"& sClassName &"' and ParentID="& nParentID        '類名重復(fù)
                    set rs=conn.execute(sql)
                    if not rs.eof then
                        FAddCheck=1
                        ErrorStr="類名重復(fù)!"
                        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")        '查找當(dāng)前數(shù)據(jù)庫中最大的類別id,如果沒有數(shù)據(jù)則設(shè)置為0,要插入的類別id為當(dāng)前最大id加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")        '查找當(dāng)前數(shù)據(jù)庫中最大的根id,如果沒有數(shù)據(jù)則設(shè)置為0,要插入的根id為當(dāng)前最大根id加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)    '查找父類別相應(yīng)信息
        if not rs.eof then
            nRootID=trim(rs("Rootid"))        '根id與父類別根id相同
            sParentPath=trim(rs("ParentPath"))& "," &nParentID    
            if cint(trim(nParentID))>0 then            '父id大于0則有父類別,故要插入的類別的深度父類別的深度加1,父id不大于0則當(dāng)前要插入的類別為根類別,則深度為0
                nDepth=cint(trim(rs("Depth")))+1
            else
                nDepth=0
            end if
            if cint(trim(rs("Child")))>0 then
                dim rsPrevOrderID
                '得到與本欄目同級的最后一個欄目的OrderID
                set rsPrevOrderID=conn.execute("select Max(OrderID) From ArticleClass where ParentID=" & ParentID)
                prevOrderID=rsPrevOrderID(0)
                '得到同一父欄目但比本欄目級數(shù)大的子欄目的最大OrderID,如果比前一個值大,則改用這個值。
                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
        '更新其父類的子欄目數(shù)
            conn.execute("update ArticleClass set child=child+1 where ClassID="& nParentID)

        '更新該欄目排序以及大于本需要和同在本分類下的欄目排序序號
            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()    '類別修改檢查函數(shù),結(jié)果為0表示通過檢查,為1表示有錯誤發(fā)生,有錯誤發(fā)生時退出函數(shù),將錯誤信息寫入錯誤變量ErrorStr
        dim temprs
        FEditCheck=0
        if nClassID="" then                    '類別id為空
            FEditCheck=1
            ErrorStr="類別id不能為空!"
            exit Function
        else                
            if sClassName="" then            '類名為空
                FEditCheck=1
                ErrorStr="類名不能為空!"
                exit Function
            else
                if nParentID<>0 then
                    set temprs=conn.execute("select ClassID From ArticleClass where ClassID=" & nParentID)        '父類別不存在
                    if temprs.eof then
                        FAddCheck=1
                        ErrorStr="所屬類別不存在或已經(jīng)被刪除!"
                        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                '類名重復(fù)
                            FEditCheck=1
                            ErrorStr="類名重復(fù)!"
                            exit Function
                        end if
                        set rs=nothing
                    end if
                    set temprs=nothing
                end if
            end if
        end if
    End Function

    Public Sub SEdit()        '類別修改
        sql="update ArticleClass set ClassName='"& sClassName &"',FilePath='"& sFilePath &"' where ClassID="& nClassID
        conn.execute(sql)
    End Sub

    Public Function FDeleteCheck()    '類別刪除檢查函數(shù),結(jié)果為0表示通過檢查,為1表示有錯誤發(fā)生,有錯誤發(fā)生時退出函數(shù),將錯誤信息寫入錯誤變量ErrorStr
        FDeleteCheck=0                '這里刪除沒有寫級聯(lián)刪除文章部分的代碼,刪除時應(yīng)該級聯(lián)刪除
        if nClassID="" then
            FDeleteCheck=1
            ErrorStr="要刪除的類別id不能為空!"
            exit Function
        else
            set rs=conn.execute("select Child from ArticleClass where ClassID="& nClassID)
            if rs.bof and rs.eof then
                FDeleteCheck=1
                ErrorStr="類別不存在或者已經(jīng)被刪除!"
                exit Function
            else
                if trim(rs("Child"))>0 then
                    FDeleteCheck=1
                    ErrorStr="該類別含有子類別,請刪除其子類別后再進(jìn)行刪除本類別的操作!"
                    exit Function
                end if
            end if
        end if
    End Function

    Public Sub SDelete()
        if nDepth>0 then            '修改父id孩子數(shù)
            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 Function FErrStr()
        FErrStr=ErrorStr    
    End Function

End Class
%>


核心js代碼 

復(fù)制代碼 代碼如下:


var xmlHttp; //定義一個全局變量
var currentID=1;//設(shè)置當(dāng)前選中ID,如果此ID不存在則會發(fā)生js錯誤
//類別顯示主函數(shù)
//cid--子類別所在層id
//id --類別id
//pid--[+]和[-]圖標(biāo)id
//fid--類別圖標(biāo)id
function DivDisplay(cid,id,pid,fid)
{
    if (GetId(cid).style.display=='')    //子類別不顯示時圖標(biāo)顯示控制
    {
        GetId(cid).style.display='none';
        GetId(pid).src = 'images/closed.gif';
        GetId(fid).src = 'images/folder.gif';
    }
    else        //展開子類別時的操作
    {
        GetId(cid).style.display='';
        GetId(pid).src = 'images/opened.gif';
        GetId(fid).src = 'images/folderopen.gif';
        if (GetId(cid).innerHTML==''||GetId(cid).innerHTML=='正在提交數(shù)據(jù)...')
        {
            GetId(cid).innerHTML='';
            ShowChild(cid,id);        //調(diào)用顯示子類別函數(shù)
        }
    }
}
//與上一個函數(shù)作用相同,只作用在最后一個類別
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=='正在提交數(shù)據(jù)...')
        {
            GetId(cid).innerHTML='';
            ShowChild(cid,id);
        }
    }
}
//類別添加函數(shù)
//id--類別id
function ClassAdd(id){
if (GetId("p"+id).src.indexOf("last")>0){    //最后一個類別時的添加操作
    if (!GetId("p"+id).onclick){
        GetId("p"+id).onclick=function (){DivDisplay2("c"+id,id,"p"+id,"f"+id);};    //為[+]和[-]添加單擊事件
        GetId("s"+id).ondblclick=function (){DivDisplay2("c"+id,id,"p"+id,"f"+id);};    //為顯示類別文字的span添加雙擊事件
        GetId("p"+id).src = 'images/lastopen.gif';
        }
    }
else{
    if (!GetId("p"+id).onclick){    //不為最后一個類別的添加操作
        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);
}
//類別修改函數(shù)
function ClassEdit(id,classname){
GetId("s"+id).innerHTML=classname;
}
//有多個子類別的類別的刪除函數(shù)
function ClassDel(id){
ShowChild("c"+id,id);
CurrentSelect(currentID,id)
BrowseRight(id);
}
//只有一個子類別的類別的刪除函數(shù)
function ClassDel1(id){
if (GetId("p"+id).src.indexOf("last")>0){        //當(dāng)類別是當(dāng)前類別的最后一個類別時
    GetId("p"+id).style.cursor="cursor";        //設(shè)置圖標(biāo)的鼠標(biāo)經(jīng)過樣式
    GetId("p"+id).onclick=function (){};        //因?yàn)橹挥幸粋€子類別刪除后就不再有子類別,故將圖標(biāo)單擊事件修改為空函數(shù)
    GetId("s"+id).ondblclick=function (){};        //同上
    GetId("p"+id).src = 'images/lastnochild.gif';    //圖標(biāo)設(shè)置
    }
else{
    GetId("p"+id).style.cursor="cursor";        //非最后一個類別的刪除操作
    GetId("p"+id).onclick=function (){};
    GetId("s"+id).ondblclick=function (){};
    GetId("p"+id).src = 'images/nofollow2.gif';        //這里的圖標(biāo)設(shè)置與前面不一樣
    }
ShowChild("c"+id,id);
CurrentSelect(currentID,id);
BrowseRight(id);
}
//向右邊框架傳遞參數(shù)
function BrowseRight(id){
CurrentSelect(currentID,id);
top.ContentFrame.location="../ArticleMain.asp?ClassID="+ id;
}
//設(shè)置類別選中狀態(tài)的函數(shù)
function CurrentSelect(oldid,newid){
currentID=newid;
document.getElementById("s"+oldid).style.backgroundColor="white";
document.getElementById("s"+currentID).style.backgroundColor="#C0C0E9";
}
//創(chuàng)建XMLHttpRequest對象
function CreateXMLHttpRequest()
{
    if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        xmlHttp = new XMLHttpRequest();
    }
}
//Ajax處理函數(shù)
//id,層id
//rid,數(shù)據(jù)在表中的id
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='出錯:'+xmlHttp.statusText;
             }
           }
           else
           {
                GetId(cid).innerHTML="正在提交數(shù)據(jù)...";
            }
          }

     }
     else
     {
         GetId(cid).innerHTML='抱歉,您的瀏覽器不支持XMLHttpRequest,請使用IE6以上版本!';
     }

}
//取得頁面對象
//id,層id
function GetId(id)
{
    return document.getElementById(id);

到此,相信大家對“ajax+asp怎么實(shí)現(xiàn)無限級分類樹型結(jié)構(gòu)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

當(dāng)前標(biāo)題:ajax+asp怎么實(shí)現(xiàn)無限級分類樹型結(jié)構(gòu)
瀏覽路徑:http://muchs.cn/article38/pphcpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、營銷型網(wǎng)站建設(shè)、商城網(wǎng)站、品牌網(wǎng)站建設(shè)全網(wǎng)營銷推廣、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司