谁会做zTree。 树菜单选中保存进数据库。下次加载树菜单打开时,还是显示的是:勾选着上次选中保存的

谁会做zTree。 树菜单选中保存进数据库。下次加载树菜单打开时,还是显示的是:勾选着上次选中保存的,第1张

数据已经保存进数据库了是吧!

然后只需要在查询树的方法里写sql

sql包含树中展示的所有数据以及选中的数据,只需在选中的数据后面拼接checked:true就行了

第一种:单表。

列分别为 id、菜单名称、子菜单id列表

id:菜单的唯一编号

子菜单id列表:用逗号(或其他分隔符)分割的多个id的字符串,再由程序中进行处理,分割出多个id;

优点:查询速度非常快。

缺点:不利于维护和拓展,在进行菜单项排序时需要再多写代码进行处理。

第二种:单表内连接。

列分别为 id、菜单名称、父菜单id

根节点的父菜单id为null或者0等一个可以唯一标志的。在查询时进行表内连接查询;

优点:利于表结构的维护和拓展,并且符合设计范式;

缺点:在查询时会造成一些麻烦,并且性能上会受到影响。

当然还有很多其他的方法,这里只例举2个比较常用的方法。

<SCRIPT LANGUAGE="JavaScript">

<!--

function CreateSelect(_FormName,_SName,_SValue,_Ds,_AllOptionStr,ShowType)

{

//三级关链菜单通用版

//作者:海娃, 有问题请到http://www.51windows.Net留言。

if (_FormName=="")

_FormName = "all"

var _DsArr = _Ds.split("|")

var _Ds1,_Ds2,_Ds3

var _Ds1 = _DsArr[0]

var _Ds2 = (_DsArr.length>1)?_DsArr[1]:_DsArr[0]

var _Ds3 = (_DsArr.length>2)?_DsArr[2]:_DsArr[0]

var _SNameArr = _SName.split("|")

var _SName1,_SName2,_SName3

var _SName1 = _SNameArr[0]

var _SName2 = (_SNameArr.length>1)?_SNameArr[1]:_SNameArr[0]

var _SName3 = (_SNameArr.length>2)?_SNameArr[2]:_SNameArr[0]

var _SValueArr = _SValue.split("|")

var _SValue1,_SValue2,_SValue3

var _SValue1 = _SValueArr[0]

var _SValue2 = (_SValueArr.length>1)?_SValueArr[1]:_SValueArr[0]

var _SValue3 = (_SValueArr.length>2)?_SValueArr[2]:_SValueArr[0]

if (ShowType==3){

_AllOptionStr = _AllOptionStr.replace(/\(\(/ig,"\(\("+_Ds3+"\^")

_AllOptionStr = _AllOptionStr.replace(/\{\{/ig,"\{\{"+_Ds2+"\(\("+_Ds3+"\*\*")

}

else if(ShowType==2){

_AllOptionStr = _AllOptionStr.replace(/\{\{/ig,"\{\{"+_Ds2+"\(\("+_Ds3+"\*\*")

}

else if(ShowType==1){

_AllOptionStr = _AllOptionStr.replace(/\(\(/ig,"\(\("+_Ds3+"\^")

}

var AllStr = _Ds1 + "{{"+ _Ds2 + "(("+ _Ds3 +"||"+_AllOptionStr

//alert(AllStr)

var _AR0 = AllStr.split("||")

document.writeln("<select name=\"" + _SName1 + "\" size=\"1\" onChange=\""+_SName1+"redirect(this.options.selectedIndex)\">")

for (var i1 = 0i1 <_AR0.lengthi1++)

{

var Area1Str = _AR0[i1]

var _AR10 = Area1Str.split("{{")

var _AR11 = _AR10[0].split("@")

var Tstr1 = _AR11[0]

var Vstr1 = (_AR11.length==2)?_AR11[1]:_AR11[0]

document.writeln("<option value=\""+Vstr1+"\">"+Tstr1+"<\/option>")

}

document.writeln("<\/select>")

document.writeln("<select name=\"" + _SName2 + "\" size=\"1\" onChange=\""+_SName1+"redirect1(this.options.selectedIndex)\">")

var _AR111 = _Ds2.split("@")

var Tstr11 = _AR111[0]

var Vstr11 = (_AR111.length==2)?_AR111[1]:_AR111[0]

document.writeln("<option value=\""+Vstr11+"\">"+Tstr11+"<\/option>")

document.writeln("<\/select>")

document.writeln("<select name=\"" + _SName3 + "\" size=\"1\">")

var _AR222 = _Ds3.split("@")

var Tstr22 = _AR222[0]

var Vstr22 = (_AR222.length==2)?_AR222[1]:_AR222[0]

document.writeln("<option value=\""+Vstr22+"\">"+Tstr22+"<\/option>")

document.writeln("<\/select>")

document.writeln("<S"+"CRIPT LANGUAGE=\"JavaScript\" defer>")

document.writeln("<!--")

document.writeln("var "+_SName1+"NewAllStr = \""+ AllStr +"\"")

document.writeln("var "+_SName1+"_AR0 = "+_SName1+"NewAllStr.split(\"||\")")

document.writeln("var "+_SName1+"groups=document."+ _FormName +"." + _SName1 + ".options.length")

document.writeln("var "+_SName1+"group=new Array("+_SName1+"groups)")

document.writeln("for (i=0i<"+_SName1+"groupsi++){")

document.writeln(" "+_SName1+"group[i]=new Array()")

document.writeln("}")

document.writeln("for (var i1 = 0i1 <"+_SName1+"_AR0.lengthi1++){")

document.writeln(" var Area1Str = "+_SName1+"_AR0[i1]")

document.writeln(" var _AR10 = Area1Str.split(\"{{\")")

document.writeln(" var _AR12 = _AR10[1].split(\"**\")")

document.writeln(" for (var i2 = 0i2 <_AR12.lengthi2++){")

document.writeln(" var Area2Str = _AR12[i2]")

document.writeln(" var _AR20 = Area2Str.split(\"((\")")

document.writeln(" var _AR211 = _AR20[0].split(\"@\")")

document.writeln(" var Tstr2 = _AR211[0]")

document.writeln(" var Vstr2 = (_AR211.length==2)?_AR211[1]:_AR211[0]")

document.writeln(" "+_SName1+"group[i1][i2]=new Option(Tstr2,Vstr2)")

document.writeln(" }")

document.writeln("}")

document.writeln("var "+_SName1+"temp = document."+ _FormName +"." + _SName2)

document.writeln("function "+_SName1+"redirect(x){")

document.writeln(" for (m="+_SName1+"temp.options.length-1m>0m--)")

document.writeln(" "+_SName1+"temp.options[m]=null")

document.writeln(" for (i=0i<"+_SName1+"group[x].lengthi++){")

document.writeln(" "+_SName1+"temp.options[i]=new Option("+_SName1+"group[x][i].text,"+_SName1+"group[x][i].value)")

document.writeln(" }")

document.writeln(" "+_SName1+"temp.options[0].selected=true")

document.writeln(" "+_SName1+"redirect1(0)")

document.writeln("}")

document.writeln("var "+_SName1+"Group2s=document."+ _FormName +"." + _SName2 + ".options.length")

document.writeln("var "+_SName1+"Group2=new Array("+_SName1+"groups)")

document.writeln("for (i=0i<"+_SName1+"groupsi++){")

document.writeln(" "+_SName1+"Group2[i]=new Array("+_SName1+"group[i].length)")

document.writeln(" for (j=0j<"+_SName1+"group[i].lengthj++){")

document.writeln(" "+_SName1+"Group2[i][j]=new Array()")

document.writeln(" }")

document.writeln("}")

document.writeln("for (var i1 = 0i1 <"+_SName1+"_AR0.lengthi1++){")

document.writeln(" var Area1Str = "+_SName1+"_AR0[i1]")

document.writeln(" var _AR10 = Area1Str.split(\"{{\")")

document.writeln(" var _AR12 = _AR10[1].split(\"**\")")

document.writeln(" for (var i2 = 0i2 <_AR12.lengthi2++){")

document.writeln(" var Area2Str = _AR12[i2]")

document.writeln(" var _AR20 = Area2Str.split(\"((\")")

document.writeln(" _AR212 = _AR20[1].split(\"^\")")

document.writeln(" for (var i3 = 0i3 <_AR212.lengthi3++){")

document.writeln(" Area3Str = _AR212[i3]")

document.writeln(" _AR3 = Area3Str.split(\"@\")")

document.writeln(" Tstr3 = _AR3[0]")

document.writeln(" Vstr3 = (_AR3.length==2)?_AR3[1]:_AR3[0]")

document.writeln(" "+_SName1+"Group2[i1][i2][i3]=new Option(Tstr3,Vstr3)")

document.writeln(" }")

document.writeln(" }")

document.writeln("}")

document.writeln("var "+_SName1+"temp1=document."+ _FormName +"." + _SName3 + "")

document.writeln("function "+_SName1+"redirect1(y){")

document.writeln(" for (m="+_SName1+"temp1.options.length-1m>0m--)")

document.writeln(" "+_SName1+"temp1.options[m]=null")

document.writeln(" for (i=0i<"+_SName1+"Group2[document."+ _FormName +"." + _SName1 + ".options.selectedIndex][y].lengthi++){")

document.writeln(" "+_SName1+"temp1.options[i]=new Option("+_SName1+"Group2[document."+ _FormName +"." + _SName1 + ".options.selectedIndex][y][i].text," + _SName1 + "Group2[document."+ _FormName +"." + _SName1 + ".options.selectedIndex][y][i].value)")

document.writeln(" }")

document.writeln(" "+_SName1+"temp1.options[0].selected=true")

document.writeln("}")

document.writeln("\/\/-->")

document.writeln("<\/script>")

document.writeln("<s"+"cript language=\"JavaScript\">")

document.writeln("<!--")

document.writeln("function "+_SName1+"SetValue(){")

document.writeln(" try{")

document.writeln(" if (\""+_SValue1+"\"!=\"\"){")

document.writeln(" document."+ _FormName +"."+_SName1+".value=\""+_SValue1+"\"")

document.writeln(" "+_SName1+"redirect(document."+ _FormName +"."+_SName1+".options.selectedIndex)")

document.writeln(" if (\""+_SValue2+"\"!=\"\"){")

document.writeln("document."+ _FormName +"."+_SName2+".value=\""+_SValue2+"\"")

document.writeln(""+_SName1+"redirect1(document."+ _FormName +"."+_SName2+".options.selectedIndex)")

document.writeln("if (\""+_SValue3+"\"!=\"\")")

document.writeln(" document."+ _FormName +"."+_SName3+".value=\""+_SValue3+"\"")

document.writeln(" }")

document.writeln(" }")

document.writeln(" }")

document.writeln(" catch(e){")

document.writeln(" }")

document.writeln("}")

document.writeln("window.attachEvent(\"onload\","+_SName1+"SetValue)")

document.writeln("\/\/-->")

document.writeln("<\/script>")

}

//-->

</script>

<%

set conn=Server.CreateObject("ADODB.Connection")

dbpath=Server.MapPath("db1.mdb")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0Jet OLEDB:Database Password=Data Source=" &dbpath

dim SelectStr

SelectStr = ""

dim sql1,sql2,sql3

dim rs1,rs2,rs3

dim i1,i2,i3

'使用缓存,application("liststr"),将添加,编辑或删除一个数据时,用application("liststr") = empty清空缓存

application("liststr") = empty

if isempty(application("liststr")) then

'//一级分类开始

sql1 = "select c1id,c1name from class1"

set rs1 = conn.execute(sql1)

i1 = 0

while not rs1.eof

if i1 >0 then

SelectStr = SelectStr &"||"

end if

SelectStr = SelectStr &rs1("c1name") &"{{"

'显示与值不同时,用下面一行

'SelectStr = SelectStr &rs1("c1name") &"@"&rs1("c1id") &"{{"

'//二级分类开始

sql2 = "select c2id,c2name from class2 where c1name = '"&replace(rs1("c1name"),"'","''") &"'"

set rs2 = conn.execute(sql2)

i2 = 0

while not rs2.eof

if i2 >0 then

SelectStr = SelectStr &"**"

end if

SelectStr = SelectStr &rs2("c2name") &"(("

'显示与值不同时,用下面一行

'SelectStr = SelectStr &rs2("c2name") &"@"&rs2("c2id") &"(("

'//三级分类开始

sql3 = "select c3id,c3name from class3 where c2name = '"&replace(rs2("c2name"),"'","''") &"'"

set rs3 = conn.execute(sql3)

i3 = 0

while not rs3.eof

if i3 >0 then

SelectStr = SelectStr &"^"

end if

SelectStr = SelectStr &rs3("c3name")

'显示与值不同时,用下面一行

'SelectStr = SelectStr &rs3("c3name") &"@" &rs3("c3id")

i3 = i3 + 1

rs3.movenext

wend

rs3.close

set rs3 = nothing

'//三级分类结束

i2 = i2 + 1

rs2.movenext

wend

rs2.close

set rs2 = nothing

'//二级分类结束

i1 = i1 + 1

rs1.movenext

wend

rs1.close

set rs1 = nothing

application("liststr") = SelectStr

end if

'//一级分类结束

%>

<SCRIPT LANGUAGE="JavaScript">

<!--

var AllStr1 = "<% = application("liststr") %>"

//-->

</SCRIPT>

<FORM name="HwForm">

<HR>选择一次<br>

<SCRIPT LANGUAGE="JavaScript">

CreateSelect("","S_13|S_23|S_33","","请选择地区@|请选择省份@|请选择城市@",AllStr1,0)

</SCRIPT>

<HR>选择三次<br>

<SCRIPT LANGUAGE="JavaScript">

CreateSelect("HwForm","S_1|S_2|S_3","华北地区|山东|济宁","请选择地区@|请选择省份@|请选择城市@",AllStr1,3)

</SCRIPT>

</FORM>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6827838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存