puff puff che faticaccia che ho fatto
codice:
<div style="width:230px; overflow:auto; position:relative; top:0px; left:0px;">
<%
dim dMenu
dim toStamp 'conterrà il menu
oConn ""
oRs "rs","select id,idgenitore,voce,link,ordine,flag from cms_menu order by idgenitore asc,flag asc,ordine asc",1,1
conta=1
set dMenu=server.CreateObject("Scripting.Dictionary")
while not rs.eof
if clng(rs(0))=clng(rs(1)) then
redim preserve testeSerie(conta)
testeSerie(conta-1)=rs(0)
conta=conta+1
end if
idchiave=clng(rs(0))
stringa=rs(0) & "|" & rs(1) & "|" & rs(2) & "|" & rs(3) & "|" & rs(4) & "|" & rs(5)
dMenu.Add idchiave,stringa
idchiave="": stringa=""
rs.movenext
wend
cRs rs: cConn
sub creaMenu(categoria)
stampaVoce categoria,1
end sub
sub stampaVoce(categoria,tipo)
dim arrT
arrT=split(dMenu.item(categoria),"|")
if ubound(arrT)>0 then
select case tipo
case 1 'è genitore
toStamp=toStamp & vbcrlf & "<div style=""" & stileGenitore & """>"
toStamp=toStamp & vbcrlf & "<div style=""float:left; width:10px"">menu" & categoria & "');"">" target="_blank"></div><div style=""position:relative;left:3px;top:-2px""><a href=""" & arrT(3) & """>" & toLow(arrT(2)) & "</div></div>" & vbcrlf
if hasFigli(categoria) then stampaFigli categoria
case else 'è figlio
toStamp=toStamp & vbcrlf & "<div style=""" & stileFiglio & """>" & toLow(arrT(2)) & "</div>" & vbcrlf
end select
end if
erase arrT
end sub
function hasFigli(categoria)
dim chiavi,rit
chiavi=dMenu.keys
rit=false
for each chiave in chiavi
str=dMenu.item(chiave)
pos1=instr(1,str,"|")
pos2=instr(pos1+1,str,"|")
if clng(left(str,pos1-1))<>clng(categoria) and clng(mid(str,pos1+1,(pos2-pos1)-1))=clng(categoria) then 'è il figlio
rit=true
end if
next
erase chiavi
hasFigli=rit
end function
sub stampaFigli(categoria)
dim chiavi
chiavi=dMenu.Keys
toStamp=toStamp & vbcrlf & "<div id=""menu" & categoria & """ style=""padding-left:20px;display:none"">"
for each chiave in chiavi
arrT=split(dMenu.item(chiave),"|")
if clng(arrT(0))<>clng(categoria) and clng(arrT(1))=clng(categoria) then stampaVoce clng(arrT(0)),cint(arrT(5))
next
toStamp=toStamp & vbcrlf & "</div>"
erase chiavi: erase arrT
end sub
'*************************
'prendo la prima voce dell'array che abbia id=idgenitore e la stampo, e controllo che abbia figli e stampo a sua volta (ricorsivamente)
'così via fino alle ennesima voce nell'array che abbia id=idgenitore
'*************************
'scorro tutte le teste di serie, per ogni testa di serie stampo il menu
for f=0 to ubound(testeSerie)
creaMenu testeSerie(f)
next
response.Write(toStamp)
%>
</div>
un accesso al db=stampo tutto il menu
legenda:
-oConn,oRs, cConn,cRs sono funzioni per aprire/chiudere recordset/connessioni
-gli elementi che avranno id=idgenitore saranno quelli visibili all'inizio (le root dell'albero)
-flag=1, è un Genitore, flag=0 è un figlio (e quindi non perdo tempo a cercare se ha figli)
-lo javascript se serve a qualcuno lo posto, altrimenti potete farlo da soli
-testeSerie è un array che contiene tutti gli id dei record che hanno id=idgenitore
se qualcuno ha un'idea migliore è ben accetta