乔客论坛 - [class.nav] - :[推荐]很不错的树形菜单代码(读取数据库)(XML)
RSS 2.0 支持
本版版主
您是本贴第 1494 位浏览者

浏览贴子: :[推荐]很不错的树形菜单代码(读取数据库)(XML) 打印 收藏 推荐 刷新

anonymous
  • 等级
  • 发贴153
  • 积分256
  • 货币11.0乔币
  • 注册2003-10-29
:[推荐]很不错的树形菜单代码(读取数据库)(XML)
我已经用asp从数据库动态修改树形菜单了
数据库结构:



<HTML>
<HEAD>
<TITLE> DSTree </TITLE>
<META NAME="Author" CONTENT="sTarsjz@hotmail.com" >
<LINK href="images/mm.css" type=text/css rel=stylesheet>
<style>
#treeBox .ec{margin:0 0 0 0;}
#treeBox .hasItems{font-weight:bold;height:20px;padding:0 0 0 0;margin:2px;cursor:hand;border:1px}
#treeBox .Items{height:20px;padding:0 0 0 0;margin:1px;cursor:hand;border:1px}
</style>
<script>
var HC = "border:1px";
var SC = "border:1px";
var IO = null;
function initTree(){
var rootn = document.all.menuXML.documentElement;
var sd = 0;
document.onselectstart = function(){return false;}
document.all.treeBox.appendChild(createTree(rootn,sd));
}
function createTree(thisn,sd){
var nodeObj = document.createElement("span");
var upobj = document.createElement("span");
with(upobj){
style.marginLeft = sd*10;
className = thisn.hasChildNodes()?"hasItems":"Items";
innerHTML = "<img src=images/e.gif class=ec>" + thisn.getAttribute("text") +"";

onmousedown = function(){
if(event.button != 1) return;
if(this.getAttribute("cn")){
this.setAttribute("open",!this.getAttribute("open"));
this.cn.style.display = this.getAttribute("open")?"inline":"none";
this.all.tags("img")[0].src = this.getAttribute("open")?"images/e.gif":"images/c.gif";
}
if(IO){
IO.runtimeStyle.cssText = "";
IO.setAttribute("selected",false);
}
IO = this;
this.setAttribute("selected",true);
this.runtimeStyle.cssText = SC;
}
onmouseover = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = HC;
}
onmouseout = function(){
if(this.getAttribute("selected"))return;
this.runtimeStyle.cssText = "";
}
oncontextmenu = contextMenuHandle;
onclick = clickHandle;
}

if(thisn.getAttribute("treeId") != null){
upobj.setAttribute("treeId",thisn.getAttribute("treeId"));
}
if(thisn.getAttribute("href") != null){
upobj.setAttribute("href",thisn.getAttribute("href"));
}
if(thisn.getAttribute("target") != null){
upobj.setAttribute("target",thisn.getAttribute("target"));
}

nodeObj.appendChild(upobj);
nodeObj.insertAdjacentHTML("beforeEnd","<br>")

if(thisn.hasChildNodes()){
var i;
var nodes = thisn.childNodes;
var cn = document.createElement("span");
upobj.setAttribute("cn",cn);
if(thisn.getAttribute("open") != null){
upobj.setAttribute("open",(thisn.getAttribute("open")=="true"));
upobj.getAttribute("cn").style.display = upobj.getAttribute("open")?"inline":"none";
if( !upobj.getAttribute("open"))upobj.all.tags("img")[0].src ="images/c.gif";
}

for(i=0;i<nodes.length;cn.appendChild(createTree(nodes[i++],sd+1)));
nodeObj.appendChild(cn);
}
else{
upobj.all.tags("img")[0].src ="images/mail_title.gif";
}
return nodeObj;
}
window.onload = initTree;
</script>

<script>
function clickHandle(){
    event.returnValue=true;
var url=this.getAttribute("href");
var targetwin=this.getAttribute("target");
if (url!=null)    {  window.open(url,targetwin,""); }

}
function contextMenuHandle(){
event.returnValue = false;
var treeId = this.getAttribute("treeId");
}
</script>
</HEAD>
<BODY text=#000000 leftMargin=0 background=images/left_bg0.gif topMargin=0 marginheight="0" marginwidth="0">
<% Call MenuCode("p_menu")%>
<TABLE height="100%" cellSpacing=0 cellPadding=0 width=154 border=0>
  <TBODY>
  <TR>
    <TD colSpan=3 height=6><br>      <SPACER height="6" type="block"></TD>
  </TR>
  <TR>
    <TD width=6><SPACER type="block" width="6"></TD>
    <TD class=leftbg vAlign=top align=left>
  <table widht='100%' border='0'>
      <tr><td id=treeBox valign=top align=left></td></tr>
      </table>
    </TD>
    <TD width=6><SPACER type="block" width="6"></TD></TR>
  <TR>
    <TD colSpan=3 height=6><SPACER height="6" type="block"></TD>
  </TR>
  </TBODY>
</TABLE>
</BODY>
</HTML>
<%
sub MenuCode(data_name)
  response.write "<xml id=menuXML>" & vbcrlf
  response.write "<?xml version='1.0' encoding='GB2312'?>"& vbcrlf
  '写树形目录的根节点
  dim strNodeCode,strNodeList
  dim treeid
  treeid=0

  call child_node(treeid,data_name)

  response.write "</DSTreeRoot>" & vbcrlf
  response.write "</xml>" & vbcrlf

end sub

sub child_node(node_id,data_name)
  dim sqlnode,rsnode
  set rsnode=server.createobject("adodb.recordset")
  'if rs<>nothing then set rsnode=rs
  'sqlnode="select * from " & data_name & " where m_id=" & node_id
  'set rsnode=joekoe_cms.exec(sqlnode,1)
  'if rsnode.eof then
  '   response.write "数据库菜单链读取错误,请检查数据库"& data_name & "中id为" & node_id &"的记录!"
  ' rsnode.close
  ' response.end
  'else
     if node_id=0 then '如果是根节点
         sqlnode="select * from " & data_name & " where m_par=" & node_id
         set rsnode=joekoe_cms.exec(sqlnode,1)
     response.write "<DSTreeRoot text='"& rsnode("m_name") & "' open='"
         if rsnode("m_open") then
            response.write "true"
         else
            response.write "false"
         end if
         response.write  "' treeId='"&rsnode("m_id")&"'>" & vbcrlf
call child_node(rsnode("m_id"),data_name)
     else '如果不是根节点
         sqlnode="select * from " & data_name & " where m_par=" & node_id
         set rsnode=joekoe_cms.exec(sqlnode,1)
         while not rsnode.eof
      dim rs_temp,sql_temp
  sql_temp="select count(0) from " & data_name & " where m_par=" & rsnode("m_id")
  set rs_temp=server.createobject("adodb.recordset")
  set rs_temp=joekoe_cms.exec(sql_temp,1)
  if rs_temp(0)=0 then
     response.write "<DSTree text='"&rsnode("m_name")&"' href='"& rsnode("m_url")&"' target='main' treeId='"&rsnode("m_id")&"' />" & vbcrlf
              else
               response.write "<DSTree text='"&rsnode("m_name") & "' open='"
             if rsnode("m_open") then
                  response.write "true"
             else
                  response.write "false"
             end if
             response.write "' treeId='" & rsnode("m_id") & "'>" & vbcrlf
             call child_node(rsnode("m_id"),data_name)
response.write "</DSTree>" & vbcrlf
              end if
rsnode.movenext
wend
      end if
  'end if
  'if rsnode<>nothing then set rs=rsnode
  'rsnode.close
  'set rsnode=nothing
end sub
%>
 
IP 操作 Top

letle
  • 等级
  • 发贴14
  • 积分26
  • 货币11.0乔币
  • 注册2005-7-23
能不能把源代码共享啊???
能不能把源代码共享啊???
 
IP 操作 Top

玫瑰天使
  • 等级
  • 发贴213
  • 积分285
  • 货币11.0乔币
  • 注册2004-1-5
还是不懂啊
QQ:26141550 CMS群:23830554免费激情片给我留言
 
IP 操作 Top

  • loading..

loading..