Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

xanburzum

[Resolvido] Menu Treeview Dinâmico

Recommended Posts

criar menu estilo treeview dinamicamente com seus dados hierarquicos facilmente ... ele funciona sem refresh. Você deve adicionar itens do menu na tabela menu , em seguida, definir as relações com campo ParentID nesta tabela.

 

em uma pasata chamada db, com o arquivo, menu.mdb, tendo a seguinte estrutura:

 

tabela:

Menu

 

Campos:

MenuId - Numeração Automática

ParentId - Número

MenuName - Texto

MenuLink - Texto

MenuOrder - Número

MenuTarget - Texto

 

 

arquivo teste.asp

<%
Dim first_rs
Dim first_rs_numRows

Set first_rs = Server.CreateObject("ADODB.Recordset")

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("db/menu.mdb")
first_rs.ActiveConnection = strConn

first_rs.Source = "SELECT MenuId,MenuName,MenuLink,MenuTarget FROM  Menu  WHERE  (ParentId = 0) ORDER BY MenuOrder"
first_rs.CursorType = 0
first_rs.CursorLocation = 2
first_rs.LockType = 1
first_rs.Open()
first_rs_numRows = 0
%>
<html>
<head>
<title>TESTE</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1254">
<style>
a:link, a:active, a:hover, a:visited {
	text-decoration: none;
   color: #000000;
	font-size: 11 px;
	font-variant: normal;
	font-weight: bold;
	font-family: Arial;
	font-style: normal;
}
tree{background:#000;}
</style>


</head>
<body link="#666666" vlink="#666666" alink="#666666">
<XML id="dsoELEMANLAR"></XML> 
<script language="Javascript">
function redirect(URLStr) { location = URLStr; }
function expand(par,iorder,icount) {
			var currNode;
			var imgop_node='fopen.gif';
			var imgcl_node='fclose.gif';
			dsoELEMANLAR.async = false; 
		
			dsoELEMANLAR.SRC="tree_process.asp?strID=" + par
		
			objNodeListMenuId = dsoELEMANLAR.getElementsByTagName("MenuId");
			objNodeListMenuName = dsoELEMANLAR.getElementsByTagName("MenuName"); 
			objNodeListMenuLink = dsoELEMANLAR.getElementsByTagName("MenuLink"); 
			objNodeListMenuTarget = dsoELEMANLAR.getElementsByTagName("MenuTarget"); 
			objNodeListcounter = dsoELEMANLAR.getElementsByTagName("counter"); 
			objNodeListitemcount = dsoELEMANLAR.getElementsByTagName("itemcount"); 
			objNodeListitemorder = dsoELEMANLAR.getElementsByTagName("itemorder"); 
			str='navLink'+par;
				 str2='picker'+par;	   
			if (document.all(str).style.display=='none') {
			
			if (objNodeListMenuId.length>0){
				document.all(str).style.display="";
				document.all(str2).src=imgop_node;
				for (var i=0; i < objNodeListMenuId.length; i++) {
				
				for (var j=0; j  <(objNodeListcounter.item(i).text-1); j++)
				{
					if(j<objNodeListcounter.item(i).text-2)
					{
					 document.all(str).innerHTML+='<img src="icon_vbar.gif" width="15" height="15" border="0" alt="">';
					}
					else
					{
					if ((iorder!=icount)) document.all(str).innerHTML+='<img src="icon_vbar.gif" width="15" height="15" border="0" alt="">';
					else document.all(str).innerHTML+='<img src="icon_blank.gif" width="15" height="15" border="0" alt="">';
					}
					
				}
				
				if ((i+1)==objNodeListMenuId.length) document.all(str).innerHTML+='<img src="icon_bar.gif" width="15" height="15" border="0" alt="">';
				else document.all(str).innerHTML+='<img src="icon_tbar.gif" width="15" height="15" border="0" alt="">';
				
				
				document.all(str).innerHTML+='<a href="#" onClick="expand('+objNodeListMenuId.item(i).text+','+objNodeListitemorder.item(i).text+','+objNodeListitemcount.item(i).text+');"><img src="'+imgcl_node+'" name="picker'+objNodeListMenuId.item(i).text+'" border="0" align="middle"></a> <a href="'+objNodeListMenuLink.item(i).text+'" target="'+objNodeListMenuTarget.item(i).text+'">'+objNodeListMenuName.item(i).text+'</a>'
				document.all(str).innerHTML+='<div id="navlink'+objNodeListMenuId.item(i).text+'" style="display=none;"></div>'
				document.all(str).innerHTML+='<div style="font-size:1px;"> </div>'
				}
				}
			}
			else
			{
			document.all(str).innerHTML='';
			document.all(str).style.display="none";
			document.all(str2).src=imgcl_node;
			}		
			} 
</script>
<%
ItemCount=0
ItemOrder=1
do while not first_rs.eof
ItemCount=ItemCount+1
first_rs.movenext
loop
first_rs.MoveFirst

do while not first_rs.eof %>
<a href="#" onClick=expand(<%=(first_rs.Fields.Item("MenuId").Value)%>,<%=ItemOrder%>,<%=ItemCount%>)><img align="middle" src="fclose.gif" name="picker<%=(first_rs.Fields.Item("MenuId").Value)%>" border="0" align="absmiddle" ></a> 
<a href="<%=(first_rs.Fields.Item(2).Value)%>" target="<%=(first_rs.Fields.Item(3).Value)%>"><%=(first_rs.Fields.Item(1).Value)%></a>
<div id=navLink<%=(first_rs.Fields.Item(0).Value)%> style="display=none;"></div>

<div style="font-size:1px;"> </div>
<%
ItemOrder=ItemOrder+1
first_rs.movenext
loop
%>

<font color="#FF0000" size="1" face="Arial, Helvetica, sans-serif"><strong>- Clique (+) para ver  sub-itens, clique nos textos para os links </strong></font> 
</body>
</html>
<%
first_rs.Close()
Set first_rs = Nothing
%>

arquivo tree_process.asp

<% @ Language="VBScript" %>
<%
Response.Buffer = True
Response.ContentType = "text/xml"
'Você pode mudar encoding
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf
strSQL = Request.QueryString("strID")
strSQL2=strSQL

  
top_number=0
sub top_count()
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection =  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("db/menu.mdb")
rs.Source = "SELECT MenuId,ParentId FROM Menu WHERE MenuId ="&strSQL&" ORDER BY MenuOrder"
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 1
rs.Open()
if not rs.eof and not rs.bof then
top_number=top_number+1
strSQL=rs("ParentId")
top_count()
end if 
end sub

top_count()

strQuery="SELECT MenuId,MenuName,MenuLink,MenuTarget FROM Menu WHERE (ParentId= "& strSQL2&") ORDER BY MenuOrder" 

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

conn.ConnectionTimeout = 15
conn.CommandTimeout = 30
conn.CursorLocation = 3 

conn.open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("db/menu.mdb")
Set objRS=conn.execute(strQuery)

ItemCount=0
ItemOrder=1
do while not objRS.Eof
ItemCount=ItemCount+1
objRS.MoveNext
loop
objRs.MoveFirst
'Gravação de dados em formato XML
Response.Write "<TREE>" & vbCrLf 
  While Not objRS.EOF
	Response.Write "<OBJECT>" & vbCrLf
	For Each objField in objRS.Fields
	  strName = objField.Name
	  strValue = objField.Value
	  Response.Write "<" & strName & ">" & vbCrLf
	  Response.Write "<![CDATA["&strValue &"]]>"&vbCrLf 
	  Response.Write "</" & strName & ">" & vbCrLf
  Next
	  Response.Write "<counter>" & vbCrLf
	  Response.Write top_number+1&vbCrLf
	  Response.Write "</counter>" & vbCrLf 
	  Response.Write "<itemcount>" & vbCrLf
	  Response.Write ItemCount&vbCrLf
	  Response.Write "</itemcount>" & vbCrLf 
	  Response.Write "<itemorder>" & vbCrLf
	  Response.Write ItemOrder&vbCrLf
	  Response.Write "</itemorder>" & vbCrLf 
	Response.Write "</OBJECT>" & vbCrLf
	ItemOrder=ItemOrder+1
	objRS.MoveNext
  Wend
Response.Write "</TREE>" & vbCrLf


%>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.