废话不多说,直接上代码(因一些原因,把部分数据修改或精简了)。
XML代码:
复制代码 代码如下:
程序代码:
复制代码 代码如下:
using System.Text;
using System.Collections;
using System.Xml;
using System.Web;
using System;
///
/// CreateTree 的摘要说明
///
public class MenuTree
{
int index = 0;//菜单栏目ID索引
private ArrayList havePermission = new ArrayList();
private bool isVip = false;
///
/// 登录用户所拥有的权限
///
private ArrayList HavePermissions
{
get { return havePermission; }
set { havePermission = value; }
}
///
/// 登录用户是否是VIP
///
private bool IsVip
{
get { return isVip; }
set { isVip = value; }
}
///
/// 登录用户所拥有的权限 是否为VIP用户
///
///
///
public MenuTree(ArrayList havePermission, bool isVip)
{
this.HavePermissions = havePermission;
this.IsVip = isVip;
}
///
/// 绑定树
///
public string BindDataToTree()
{
System.Xml.Xmldocument document = new System.Xml.XmlDatadocument();
document.Load(HttpContext.Current.Server.MapPath("MenuData.xml"));
return CreateTreeHtml(document.documentElement, 0);
}
///
/// 创建栏目树
///
/// xml节点
/// 树深度
private string CreateTreeHtml(System.Xml.XmlNode document, int deep)
{
string nodeType = "Menu";//节点的类型,来生成子节点的CSS类型
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"]);//显示栏目名
string nodeId = GetAttributesValue(node.Attributes["Id"]);//栏目ID
bool isExpand = GetAttributesValue(node.Attributes["IsExpand"]).ToLower().Trim() == "true" ? true : false;//是否展开
string permissions = GetAttributesValue(node.Attributes["Permissions"]);//权限字串
bool isonlyVip = GetAttributesValue(node.Attributes["IsOnlyVip"]).ToLower().Trim() == "true" ? true : false;//是否只允许VIP访问
bool isUnVip = GetAttributesValue(node.Attributes["IsUnVip"]).ToLower().Trim() == "true" ? true : false;//是否只准非VIP访问
string eventscript = GetAttributesValue(node.Attributes["Eventscript"]);//事件脚本
int chlidNodesCount = node.ChildNodes.Count;//子节点数
bool isPermissions = GetIsPermissions(permissions);//是否有权限
if (!isPermissions)
{
continue; //如果没有权限,不生成此节点
}
if (nodeName == "Module")
{
if (isUnVip && IsVip)
{
continue;//如果为VIP会员 设为不允许访问子栏目
}
menuId = GetMenuId(nodeId);
treeHtml.AppendFormat("", menuId, eventscript);
treeHtml.Append(" ");
treeHtml.AppendFormat("{0}", showName);
treeHtml.Append("");
deep = 0;
nodeType = "Module";
}
else
{
treeHtml.Append("
| "); if (chlidNodesCount > 0) { menuId = GetMenuId(nodeId); treeHtml.AppendFormat("", menuId, (isExpand ? "open-menu" : "close-menu")); } else { treeHtml.Append(""); } treeHtml.Append(" | "); "); | "); if (url.Length > 0 || chlidNodesCount == 0) { if (!isonlyVip || (isonlyVip && IsVip))//栏目是否只为VIP开放 { if (url.Length > 0) { treeHtml.AppendFormat("{2}", url, title, showName, eventscript, menuNodeId); } else { treeHtml.AppendFormat("{1}", title, showName, eventscript, menuNodeId); } } else { treeHtml.AppendFormat("{2}", url, title, showName, menuNodeId); } } else { treeHtml.AppendFormat("{2}", menuId, title, showName, eventscript, menuNodeId); } treeHtml.Append(" | ");
}
if (chlidNodesCount > 0)
{
treeNodeHtml = CreateTreeHtml(node, deep + 1);
}
if (treeNodeHtml.Length > 0)
{
treeHtml.AppendFormat("", menuId, (nodeType == "Module" ? "class='Menus'" : "class='MenuNodes'"), (isExpand ? "display:block;" : "display: none;"));
treeHtml.Append(treeNodeHtml);
treeHtml.Append("");
}
}
return treeHtml.ToString();
}
///
/// 取得栏目的ID
///
private string GetMenuId(string nodeId)
{
return nodeId.Length > 0 ? nodeId : Convert.ToString(++index);
}
///
/// 取得节点值
///
private string GetAttributesValue(XmlAttribute attributevalue)
{
return attributevalue == null ? "" : attributevalue.Value.Trim();
}
///
/// 是否有权限
///
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;
}
}
}
相关JS代码:
复制代码 代码如下:
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("非常抱歉,此模块只对VIP会员开放!");return false;
}
最后效果
此代码我觉得易放便扩展,只要稍改动一下代码就能使用在自己的项目中。



