The data source is XML, so it can be expanded.
Let's not talk about it much. directly add code (for some reason, modify or streamline some data ).
XML code:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<MenuData>
<Module Id = "Business" Permissions = "49,53, 58,59, 65,99, 100,70, 69" Name = "Business Management" IsExpand = "true">
<Menu Id = "Orders" Permissions = "49,53, 58" Name = "My Orders" IsExpand = "true">
<Item Permissions = "49" IsOnlyVip = "true" Name = "pre-order" Url = "Business/ProList. aspx" Title = "pre-order"/>
<Item Id = "processing ing" Permissions = "53" IsOnlyVip = "true" Name = "in order" Url = "Business/List. aspx" Title = "in order"/>
<Item Permissions = "58" IsOnlyVip = "true" Name = "order history" Url = "Business/RList. aspx" Title = "order history"/>
</Menu>
<Menu Id = "Clients" Permissions = "59" Name = "my customers" IsExpand = "true">
<Item Id = "ClientList" Permissions = "59" Name = "customer List" Url = "Client/List. aspx" Title = "customer List"/>
</Menu>
<Menu Id = "" Permissions = "65,99, 100" Name = "My reports" IsExpand = "true">
<Item Permissions = "65" IsOnlyVip = "true" Name = "Income Statistics" Url = "Report/Finance. aspx" Title = "Income Statistics"/>
<Item Permissions = "99" IsOnlyVip = "true" Name = "Product statistics" Url = "Report/Product. aspx" Title = "Product statistics"/>
<Item Permissions = "100" IsOnlyVip = "true" Name = "Customer statistics" Url = "Report/Client. aspx" Title = "Customer statistics"/>
</Menu>
<Menu Id = "" Permissions = "70,69" Name = "Message Management" IsExpand = "true">
<Item Permissions = "70" Name = "intra-site message" Url = "" Title = "intra-site message" EventScript = "onclick = 'openmsgwindow () '"/>
<Item Id = "ReceiveMsgList" Permissions = "69" Name = "Message receiving list" Url = "Message/RList. aspx" Title = "Message receiving list"/>
<Item Permissions = "69" Name = "Message sending list" Url = "Message/SList. aspx" Title = "Message sending list"/>
</Menu>
</Module>
<Module Id = "Advertise" Permissions = "89,90" Name = "I want to promote" EventScript = "OpenTuiGuangIndex ();">
<Menu Id = "" Permissions = "89" Name = "Product promotion" Url = "TuiGuang/Product. aspx" Title = "Product promotion"/>
<Menu Id = "" Permissions = "90" Name = "Ad Promotion" Url = "TuiGuang/Ad. aspx" Title = "Ad Promotion"/>
</Module>
<Module Id = "SystemManage" Permissions = "67,68" Name = "system settings">
<Menu Id = "" Permissions = "67" Name = "change system Password" Url = "SysManage/Password. aspx" Title = "change system Password"/>
<Menu Id = "" Permissions = "68" Name = "System Configuration" Url = "SysManage/Config. aspx" Title = "System Configuration"/>
</Module>
<Module Id = "VIP" Permissions = "" Name = "member Introduction" IsUnVip = "true">
<Menu Id = "" Name = "member Introduction" Url = "VIP/index.html" Title = "Member Service"/>
</Module>
</MenuData>
Program code:Copy codeThe Code is as follows: using System. Text;
Using System. Collections;
Using System. Xml;
Using System. Web;
Using System;
/// <Summary>
/// Summary of CreateTree
/// </Summary>
Public class MenuTree
{
Int index = 0; // the index of the menu column ID
Private ArrayList havePermission = new ArrayList ();
Private bool isVip = false;
/// <Summary>
/// Permissions of the logon user
/// </Summary>
Private ArrayList HavePermissions
{
Get {return havePermission ;}
Set {havePermission = value ;}
}
/// <Summary>
/// Whether the logon user is a VIP
/// </Summary>
Private bool IsVip
{
Get {return isVip ;}
Set {isVip = value ;}
}
/// <Summary>
/// Whether the permissions granted by the logon user are VIP users
/// </Summary>
/// <Param name = "havePermission"> </param>
/// <Param name = "isVip"> </param>
Public MenuTree (ArrayList havePermission, bool isVip)
{
This. HavePermissions = havePermission;
This. IsVip = isVip;
}
/// <Summary>
/// Bind the tree
/// </Summary>
Public string BindDataToTree ()
{
System. Xml. XmlDocument document = new System. Xml. XmlDataDocument ();
Document. Load (HttpContext. Current. Server. MapPath ("MenuData. xml "));
Return CreateTreeHtml (document. DocumentElement, 0 );
}
/// <Summary>
/// Create a column tree
/// </Summary>
/// <Param name = "document"> xml node </param>
/// <Param name = "deep"> tree depth </param>
Private string CreateTreeHtml (System. Xml. XmlNode document, int deep)
{
String nodeType = "Menu"; // type of the node to generate the CSS type of the child node.
StringBuilder treeHtml = new StringBuilder ();
Foreach (System. Xml. XmlNode node in document. ChildNodes)
{
String menuId = string. Empty;
String treeNodeHtml = string. Empty;
String nodeName = node. Name;
String showName = GetAttributesValue (node. Attributes ["Name"]); // display the column Name
String nodeId = GetAttributesValue (node. Attributes ["Id"]); // topic ID
Bool isExpand = GetAttributesValue (node. Attributes ["IsExpand"]). ToLower (). Trim () = "true "? True: false; // whether to expand
String permissions = GetAttributesValue (node. Attributes ["Permissions"]); // permission string
Bool isOnlyVip = GetAttributesValue (node. Attributes ["IsOnlyVip"]). ToLower (). Trim () = "true "? True: false; // whether only VIP access is allowed
Bool isUnVip = GetAttributesValue (node. Attributes ["IsUnVip"]). ToLower (). Trim () = "true "? True: false; // whether to allow non-VIP access only
String eventScript = GetAttributesValue (node. Attributes ["EventScript"]); // event script
Int chlidNodesCount = node. ChildNodes. Count; // Number of subnodes
Bool isPermissions = GetIsPermissions (permissions); // check whether the permission exists.
If (! IsPermissions)
{
Continue; // if you do not have the permission, do not generate this node
}
If (nodeName = "Module ")
{
If (isUnVip & IsVip)
{
Continue; // If a VIP member is set to an inaccessible subtopic
}
MenuId = GetMenuId (nodeId );
TreeHtml. appendFormat ("<div class = 'module' id = 'menu {0} 'onclick = 'donodes (this); {1} 'onselectstart = 'Return false; '> ", menuId, eventScript );
TreeHtml. Append (" ");
TreeHtml. AppendFormat ("<span> {0} </span>", showName );
TreeHtml. Append ("</div> ");
Deep = 0;
NodeType = "Module ";
}
Else
{
TreeHtml. Append ("<table cellpadding = '0' cellspacing = '0' style = 'border-width: 0; width: 90% '> ");
TreeHtml. Append ("<tr class = 'nodeline'> ");
For (int I = 0; I <deep; I ++)
{
If (I = deep-1)
{
TreeHtml. Append ("<td class = 'nodeicobox'> ");
If (chlidNodesCount> 0)
{
MenuId = GetMenuId (nodeId );
TreeHtml. appendFormat ("<a id = 'menu {0} 'href = 'javascript:; 'onclick = 'donodes (this, \" Menu \") '> </a>", menuId, (isExpand? "Open-menu": "close-menu "));
}
Else
{
TreeHtml. Append (" ");
}
TreeHtml. Append ("</td> ");
}
Else
{
TreeHtml. Append ("<td style = 'width: 20px; '> </td> ");
}
}
String url = GetAttributesValue (node. Attributes ["Url"]); // link address
String title = GetAttributesValue (node. Attributes ["Title"]); // link TITLE information
String menuNodeId = nodeId. Trim (). Length> 0? "Id = 'menunode" + nodeId + "'": string. Empty; // Tree node ID
TreeHtml. Append ("<td style = 'white-space: nowrap; '> ");
If (url. Length> 0 | chlidNodesCount = 0)
{
If (! IsOnlyVip | (isOnlyVip & IsVip) // whether the topic is only available for VIP
{
If (url. Length> 0)
{
TreeHtml. appendFormat ("<a href = '{0}' target = 'mainframework' title = '{1}' {3} {4 }>{ 2} </a> ", url, title, showName, eventScript, menuNodeId );
}
Else
{
TreeHtml. appendFormat ("<a href = 'javascript:; 'target = 'mainframework' title = '{0}' {2} {3 }>{ 1} </a> ", title, showName, eventScript, menuNodeId );
}
}
Else
{
TreeHtml. appendFormat ("<a href = 'javascript:; 'target = 'mainframework' title = '{1} 'onclick = 'Return AlertVip (); 'class = 'disablecolor' {3 }>{ 2} </a> ", url, title, showName, menuNodeId );
}
}
Else
{
TreeHtml. appendFormat ("<a href = 'javascript:; 'onclick = 'doaclick (\" Menu {0 }\"); 'title = '{1}' {3} {4} >{2} </a> ", menuId, title, showName, eventScript, menuNodeId );
}
TreeHtml. Append ("</td> ");
TreeHtml. Append ("</tr> ");
TreeHtml. Append ("</table> ");
}
If (chlidNodesCount> 0)
{
TreeNodeHtml = CreateTreeHtml (node, deep + 1 );
}
If (treeNodeHtml. Length> 0)
{
TreeHtml. AppendFormat ("<div id = 'menu {0} Nodes '{1} style =' {2} '>", menuId, (nodeType = "Module "? "Class = 'menus'": "class = 'menunoss'"), (isExpand? "Display: block;": "display: none ;"));
TreeHtml. Append (treeNodeHtml );
TreeHtml. Append ("</div> ");
}
}
Return treeHtml. ToString ();
}
/// <Summary>
/// Obtain the column ID
/// </Summary>
Private string GetMenuId (string nodeId)
{
Return nodeId. Length> 0? NodeId: Convert. ToString (++ index );
}
/// <Summary>
/// Obtain the node Value
/// </Summary>
Private string GetAttributesValue (XmlAttribute attributeValue)
{
Return attributeValue = null? "": AttributeValue. Value. Trim ();
}
/// <Summary>
/// Whether you have the permission
/// </Summary>
Private bool GetIsPermissions (string permissions)
{
If (HavePermissions. Count = 0)
{
Return false;
}
If (permissions. Length = 0)
{
Return true;
}
Else
{
String [] arrPermissions = permissions. Split (',');
For (int I = 0; I <arrPermissions. Length; I ++)
{
If (HavePermissions. Contains (arrPermissions [I]. Trim ()))
{
Return true;
}
}
Return false;
}
}
}
Related JS Code:Copy codeThe Code is as follows: function DoNodes (obj, type)
{
Var divId = obj. id + 'nodes ';
Var qdivObj = $ ("#" + divId );
If (qdivObj. is (": hidden "))
{
QdivObj. show ();
If (type = 'menu ')
{
$ (Obj). children ("img"). attr ("src", "Images/open-menu.gif ");
}
Else
{
QdivObj. siblings (". Menus: visible"). hide ();
}
}
Else
{
QdivObj. hide ();
If (type = 'menu ')
{
$ (Obj). children ("img"). attr ("src", "Images/close-menu.gif ");
}
}
}
Function DoAClick (id)
{
$ ("#" + Id). click ();
}
Function AlertVip ()
{
Alert ("sorry, this module is only available to VIP members! "); Return false;
}
Final Effect
I think this code is easy to scale and can be used in my own project with just a few changes.