手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >ajax+asp无限级分类树型结构的代码
ajax+asp无限级分类树型结构的代码
摘要:复制代码代码如下:0thendimrsPrevOrderID'得到与本栏目同级的最后一个栏目的OrderIDsetrsPrevOrderID...

复制代码 代码如下:

<%

'数据库字段为类属性,添加、删除、修改、操作检查等函数为类的方法

ClassCls_Leibie

PrivatenClassID,sClassName,nParentID,sParentPath,nDepth,nRootID,nChild,nOrderID,sFilePath'定义私有变量(类的属性,即数据库字段对应的变量)

Privaters,sql,ErrorStr

PrivateSubClass_Initialize()

ErrorStr=""'初始化错误信息为空

EndSub

PrivateSubClass_Terminate()'销毁类时关闭数据库连接

IfIsObject(Conn)Then

Conn.Close

SetConn=Nothing

EndIf

EndSub

'*******************设置各个属性******************************************************

PublicPropertyLetClassID(str)'获取类别ID(主键)

nClassID=str

callClassProperty()'获取类别ID时调用此函数读出类的所有属性

EndProperty

PublicPropertyLetClassName(str)'获取类别名称

sClassName=str

EndProperty

PublicPropertyGetClassName

ClassName=sClassName

EndProperty

PublicPropertyLetParentID(str)'获取类别父ID

nParentID=str

EndProperty

PublicPropertyGetParentID

ParentID=nParentID

EndProperty

PublicPropertyLetParentPath(str)'获取父路径ID

sParentPath=str

EndProperty

PublicPropertyGetParentPath

ParentPath=sParentPath

EndProperty

PublicPropertyLetDepth(str)'获取类别深度

nDepth=str

EndProperty

PublicPropertyGetDepth

Depth=nDepth

EndProperty

PublicPropertyLetRootID(str)'获取类别根ID

nRootID=str

EndProperty

PublicPropertyGetRootID

RootID=nRootID

EndProperty

PublicPropertyLetChild(str)'子类别个数

nChild=str

EndProperty

PublicPropertyGetChild

Child=nChild

EndProperty

PublicPropertyLetOrderID(str)'排序ID

nOrderID=str

EndProperty

PublicPropertyGetOrderID

OrderID=nOrderID

EndProperty

PublicPropertyLetFilePath(str)'类别文件根目录(生成静态文件路径,小站奇人异事网([url]www.guaishi.org[/url])用的是生成静态,故设置此字段)

sFilePath=str

EndProperty

PublicPropertyGetFilePath

FilePath=sFilePath

EndProperty

'******************************************************************************

PrivateSubClassProperty()'读取类的所有属性

sql="select*fromArticleClasswhereClassID="&nClassID

setrs=conn.execute(sql)

ifnotrs.eofthen

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"))

endif

setrs=nothing

EndSub

PublicFunctionFAddCheck()'类别添加检查函数,结果为0表示通过检查,为1表示有错误发生,有错误发生时退出函数,将错误信息写入错误变量ErrorStr

dimtemprs

FAddCheck=0

ifsClassName=""then'类名为空

FAddCheck=1

ErrorStr="类名不能为空!"

exitFunction

else

ifnParentID=""then'父id为空

FAddCheck=1

ErrorStr="父id不能为空!"

exitFunction

else

ifnParentID<>0then

settemprs=conn.execute("selectClassIDFromArticleClasswhereClassID="&nParentID)'父类别不存在

iftemprs.eofthen

FAddCheck=1

ErrorStr="所属类别不存在或已经被删除!"

exitFunction

else

sql="selectClassIDfromArticleClasswhereClassName='"&sClassName&"'andParentID="&nParentID'类名重复

setrs=conn.execute(sql)

ifnotrs.eofthen

FAddCheck=1

ErrorStr="类名重复!"

exitFunction

endif

setrs=nothing

endif

settemprs=nothing

else

sql="selectClassIDfromArticleClasswhereClassName='"&sClassName&"'andParentID="&nParentID'类名重复

setrs=conn.execute(sql)

ifnotrs.eofthen

FAddCheck=1

ErrorStr="类名重复!"

exitFunction

endif

setrs=nothing

endif

endif

endif

EndFunction

PublicSubSAdd()

dimmaxClassID,maxRootID

setrs=conn.execute("selectMax(ClassID)fromArticleClass")'查找当前数据库中最大的类别id,如果没有数据则设置为0,要插入的类别id为当前最大id加1

maxClassID=rs(0)

ifisnull(maxClassID)then

maxClassID=0

endif

setrs=nothing

nClassID=maxClassID+1

setrs=conn.execute("selectmax(rootid)FromArticleClass")'查找当前数据库中最大的根id,如果没有数据则设置为0,要插入的根id为当前最大根id加1

maxRootID=rs(0)

ifisnull(maxRootID)then

maxRootID=0

endif

nRootID=maxRootID+1

setrs=conn.execute("selectRootID,Depth,ParentPath,Child,OrderIDFromArticleClasswhereClassID="&nParentID)'查找父类别相应信息

ifnotrs.eofthen

nRootID=trim(rs("Rootid"))'根id与父类别根id相同

sParentPath=trim(rs("ParentPath"))&","&nParentID

ifcint(trim(nParentID))>0then'父id大于0则有父类别,故要插入的类别的深度父类别的深度加1,父id不大于0则当前要插入的类别为根类别,则深度为0

nDepth=cint(trim(rs("Depth")))+1

else

nDepth=0

endif

ifcint(trim(rs("Child")))>0then

dimrsPrevOrderID

'得到与本栏目同级的最后一个栏目的OrderID

setrsPrevOrderID=conn.execute("selectMax(OrderID)FromArticleClasswhereParentID="&ParentID)

prevOrderID=rsPrevOrderID(0)

'得到同一父栏目但比本栏目级数大的子栏目的最大OrderID,如果比前一个值大,则改用这个值。

setrsPrevOrderID=conn.execute("selectMax(OrderID)FromArticleClasswhereParentPathlike'"&ParentPath&",%'")

if(not(rsPrevOrderID.bofandrsPrevOrderID.eof))then

ifnotIsNull(rsPrevOrderID(0))then

ifrsPrevOrderID(0)>prevOrderIDthen

prevOrderID=rsPrevOrderID(0)

endif

endif

endif

setrsPrevOrderID=nothing

endif

nOrderID=prevOrderID+1

else

nOrderID=0

sParentPath="0"

nDepth=0

endif

setrs=nothing

nChild=0

sql="insertintoArticleClass(ClassID,ClassName,ParentID,ParentPath,Depth,RootID,Child,OrderID,FilePath)values("&nClassID&",'"&sClassName&"',"&nParentID&",'"&sParentPath&"',"&nDepth&","&nRootID&","&nChild&","&nOrderID&",'"&sFilePath&"')"

conn.execute(sql)

ifParentID>0then

'更新其父类的子栏目数

conn.execute("updateArticleClasssetchild=child+1whereClassID="&nParentID)

'更新该栏目排序以及大于本需要和同在本分类下的栏目排序序号

ifprevOrderID<>""then

conn.execute("updateArticleClasssetOrderID=OrderID+1whererootid="&nRootid&"andOrderID>"&prevOrderID&"andClassID<>"&nClassID)

endif

endif

EndSub

PublicFunctionFEditCheck()'类别修改检查函数,结果为0表示通过检查,为1表示有错误发生,有错误发生时退出函数,将错误信息写入错误变量ErrorStr

dimtemprs

FEditCheck=0

ifnClassID=""then'类别id为空

FEditCheck=1

ErrorStr="类别id不能为空!"

exitFunction

else

ifsClassName=""then'类名为空

FEditCheck=1

ErrorStr="类名不能为空!"

exitFunction

else

ifnParentID<>0then

settemprs=conn.execute("selectClassIDFromArticleClasswhereClassID="&nParentID)'父类别不存在

iftemprs.eofthen

FAddCheck=1

ErrorStr="所属类别不存在或已经被删除!"

exitFunction

else

setrs=conn.execute("selectClassIDfromArticleClasswhereClassName='"&sClassName&"'andClassID<>"&nClassID&"andParentID="&nParentID)

ifnotrs.eofthen'类名重复

FEditCheck=1

ErrorStr="类名重复!"

exitFunction

endif

setrs=nothing

endif

settemprs=nothing

endif

endif

endif

EndFunction

PublicSubSEdit()'类别修改

sql="updateArticleClasssetClassName='"&sClassName&"',FilePath='"&sFilePath&"'whereClassID="&nClassID

conn.execute(sql)

EndSub

PublicFunctionFDeleteCheck()'类别删除检查函数,结果为0表示通过检查,为1表示有错误发生,有错误发生时退出函数,将错误信息写入错误变量ErrorStr

FDeleteCheck=0'这里删除没有写级联删除文章部分的代码,删除时应该级联删除

ifnClassID=""then

FDeleteCheck=1

ErrorStr="要删除的类别id不能为空!"

exitFunction

else

setrs=conn.execute("selectChildfromArticleClasswhereClassID="&nClassID)

ifrs.bofandrs.eofthen

FDeleteCheck=1

ErrorStr="类别不存在或者已经被删除!"

exitFunction

else

iftrim(rs("Child"))>0then

FDeleteCheck=1

ErrorStr="该类别含有子类别,请删除其子类别后再进行删除本类别的操作!"

exitFunction

endif

endif

endif

EndFunction

PublicSubSDelete()

ifnDepth>0then'修改父id孩子数

conn.execute("updateArticleClasssetchild=child-1wherechild>0andClassID="&nParentID)

endif

sql="deletefromArticleClasswhereClassID="&nClassID

conn.execute(sql)

EndSub

PublicFunctionFErrStr()

FErrStr=ErrorStr

EndFunction

EndClass

%>

核心js代码

复制代码 代码如下:

varxmlHttp;//定义一个全局变量

varcurrentID=1;//设置当前选中ID,如果此ID不存在则会发生js错误

//类别显示主函数

//cid--子类别所在层id

//id--类别id

//pid--[+]和[-]图标id

//fid--类别图标id

functionDivDisplay(cid,id,pid,fid)

{

if(GetId(cid).style.display=='')//子类别不显示时图标显示控制

{

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=='正在提交数据...')

{

GetId(cid).innerHTML='';

ShowChild(cid,id);//调用显示子类别函数

}

}

}

//与上一个函数作用相同,只作用在最后一个类别

functionDivDisplay2(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=='正在提交数据...')

{

GetId(cid).innerHTML='';

ShowChild(cid,id);

}

}

}

//类别添加函数

//id--类别id

functionClassAdd(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);

}

//类别修改函数

functionClassEdit(id,classname){

GetId("s"+id).innerHTML=classname;

}

//有多个子类别的类别的删除函数

functionClassDel(id){

ShowChild("c"+id,id);

CurrentSelect(currentID,id)

BrowseRight(id);

}

//只有一个子类别的类别的删除函数

functionClassDel1(id){

if(GetId("p"+id).src.indexOf("last")>0){//当类别是当前类别的最后一个类别时

GetId("p"+id).style.cursor="cursor";//设置图标的鼠标经过样式

GetId("p"+id).onclick=function(){};//因为只有一个子类别删除后就不再有子类别,故将图标单击事件修改为空函数

GetId("s"+id).ondblclick=function(){};//同上

GetId("p"+id).src='images/lastnochild.gif';//图标设置

}

else{

GetId("p"+id).style.cursor="cursor";//非最后一个类别的删除操作

GetId("p"+id).onclick=function(){};

GetId("s"+id).ondblclick=function(){};

GetId("p"+id).src='images/nofollow2.gif';//这里的图标设置与前面不一样

}

ShowChild("c"+id,id);

CurrentSelect(currentID,id);

BrowseRight(id);

}

//向右边框架传递参数

functionBrowseRight(id){

CurrentSelect(currentID,id);

top.ContentFrame.location="../ArticleMain.asp?ClassID="+id;

}

//设置类别选中状态的函数

functionCurrentSelect(oldid,newid){

currentID=newid;

document.getElementById("s"+oldid).style.backgroundColor="white";

document.getElementById("s"+currentID).style.backgroundColor="#C0C0E9";

}

//创建XMLHttpRequest对象

functionCreateXMLHttpRequest()

{

if(window.ActiveXObject)

{

xmlHttp=newActiveXObject("Microsoft.XMLHTTP");

}

else

{

xmlHttp=newXMLHttpRequest();

}

}

//Ajax处理函数

//id,层id

//rid,数据在表中的id

functionShowChild(cid,id)

{

CreateXMLHttpRequest();

if(xmlHttp)

{

xmlHttp.open('POST','child.asp',true);

xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

varSendData='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="正在提交数据...";

}

}

}

else

{

GetId(cid).innerHTML='抱歉,您的浏览器不支持XMLHttpRequest,请使用IE6以上版本!';

}

}

//取得页面对象

//id,层id

functionGetId(id)

{

returndocument.getElementById(id);

}

【ajax+asp无限级分类树型结构的代码】相关文章:

jQuery ajax json 数据的遍历代码

ajax异步处理POST表单中的数据示例代码

无限分级和tree结构数据增删改【附DEMO下载】

Ajax 无刷新在注册用户名时的应用的代码

javascript Ajax 类实现代码

Ajax添加数据与删除篇实现代码

ajax传递多个参数的实现代码

琥珀无限级分类联动菜单AJAX版

通过数据库和ajax方法写出地图的实例代码

ajax智能提示+textbox动态生成下拉框示例代码

精品推荐
分类导航