asp.net 用XML生成放便扩展的自定义树

时间:2021-05-28

数据源为XML,因此放便扩展。
废话不多说,直接上代码(因一些原因,把部分数据修改或精简了)。
XML代码:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<MenuData>
<Module Id="Business" Permissions="49,53,58,59,65,99,100,70,69" Name="业务管理" IsExpand="true">
<Menu Id="Orders" Permissions="49,53,58" Name="我的订单" IsExpand="true">
<Item Permissions="49" IsOnlyVip="true" Name="预订单" Url="Business/ProList.aspx" Title="预订单"/>
<Item Id="Renting" Permissions="53" IsOnlyVip="true" Name="在订单" Url="Business/List.aspx" Title="在订单"/>
<Item Permissions="58" IsOnlyVip="true" Name="订单历史" Url="Business/RList.aspx" Title="订单历史"/>
</Menu>
<Menu Id="Clients" Permissions="59" Name="我的客户" IsExpand="true">
<Item Id="ClientList" Permissions="59" Name="客户列表" Url="Client/List.aspx" Title="客户列表"/>
</Menu>
<Menu Id="" Permissions="65,99,100" Name="我的报表" IsExpand="true">
<Item Permissions="65" IsOnlyVip="true" Name="收入统计" Url="Report/Finance.aspx" Title="收入统计"/>
<Item Permissions="99" IsOnlyVip="true" Name="产品统计" Url="Report/Product.aspx" Title="产品统计"/>
<Item Permissions="100" IsOnlyVip="true" Name="客户统计" Url="Report/Client.aspx" Title="客户统计"/>
</Menu>
<Menu Id="" Permissions="70,69" Name="消息管理" IsExpand="true">
<Item Permissions="70" Name="发送站内消息" Url="" Title="发送站内消息" EventScript="onclick='OpenMsgWindow()'" />
<Item Id="ReceiveMsgList" Permissions="69" Name="消息接收列表" Url="Message/RList.aspx" Title="消息接收列表"/>
<Item Permissions="69" Name="消息发送列表" Url="Message/SList.aspx" Title="消息发送列表"/>
</Menu>
</Module>
<Module Id="Advertise" Permissions="89,90" Name="我要推广" EventScript="OpenTuiGuangIndex();" >
<Menu Id="" Permissions="89" Name="产品推广" Url="TuiGuang/Product.aspx" Title="产品推广"/>
<Menu Id="" Permissions="90" Name="广告推广" Url="TuiGuang/Ad.aspx" Title="广告推广"/>
</Module>
<Module Id="SystemManage" Permissions="67,68" Name="系统设置">
<Menu Id="" Permissions="67" Name="更改系统密码" Url="SysManage/Password.aspx" Title="更改系统密码"/>
<Menu Id="" Permissions="68" Name="系统配置" Url="SysManage/Config.aspx" Title="系统配置"/>
</Module>
<Module Id="VIP" Permissions="" Name="会员介绍" IsUnVip="true">
<Menu Id="" Name="会员介绍" Url="VIP/index.html" Title="会员服务"/>
</Module>
</MenuData>

程序代码:
复制代码 代码如下:
using System.Text;
using System.Collections;
using System.Xml;
using System.Web;
using System;
/// <summary>
/// CreateTree 的摘要说明
/// </summary>
public class MenuTree
{
int index = 0;//菜单栏目ID索引
private ArrayList havePermission = new ArrayList();
private bool isVip = false;
/// <summary>
/// 登录用户所拥有的权限
/// </summary>
private ArrayList HavePermissions
{
get { return havePermission; }
set { havePermission = value; }
}
/// <summary>
/// 登录用户是否是VIP
/// </summary>
private bool IsVip
{
get { return isVip; }
set { isVip = value; }
}
/// <summary>
/// 登录用户所拥有的权限 是否为VIP用户
/// </summary>
/// <param name="havePermission"></param>
/// <param name="isVip"></param>
public MenuTree(ArrayList havePermission, bool isVip)
{
this.HavePermissions = havePermission;
this.IsVip = isVip;
}
/// <summary>
/// 绑定树
/// </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>
/// 创建栏目树
/// </summary>
/// <param name="document">xml节点</param>
/// <param name="deep">树深度</param>
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("<div class='Module' id='Menu{0}' onclick='DoNodes(this);{1}' onselectstart='return false;'>", menuId, eventScript);
treeHtml.Append("&nbsp;<img src='/images/sideMenuIcon.gif' alt='' />&nbsp;");
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\")'><img src='/Images/{1}.gif' alt=''/></a>", menuId, (isExpand ? "open-menu" : "close-menu"));
}
else
{
treeHtml.Append("<img src='/Images/open-menuno.gif' alt=''/>");
}
treeHtml.Append("</td>");
}
else
{
treeHtml.Append("<td style='width: 20px;'>&nbsp;</td>");
}
}
string url = GetAttributesValue(node.Attributes["Url"]); //链接地址
string title = GetAttributesValue(node.Attributes["Title"]);//链接TITLE信息
string menuNodeId = nodeId.Trim().Length > 0 ? "id='MenuNode" + nodeId + "'" : string.Empty;//树节点ID
treeHtml.Append("<td style='white-space: nowrap;'>");
if (url.Length > 0 || chlidNodesCount == 0)
{
if (!isOnlyVip || (isOnlyVip && IsVip))//栏目是否只为VIP开放
{
if (url.Length > 0)
{
treeHtml.AppendFormat("<a href='{0}' target='MainFrame' title='{1}' {3} {4}>{2}</a>", url, title, showName, eventScript, menuNodeId);
}
else
{
treeHtml.AppendFormat("<a href='javascript:;' target='MainFrame' title='{0}' {2} {3}>{1}</a>", title, showName, eventScript, menuNodeId);
}
}
else
{
treeHtml.AppendFormat("<a href='javascript:;' target='MainFrame' 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='MenuNodes'"), (isExpand ? "display:block;" : "display: none;"));
treeHtml.Append(treeNodeHtml);
treeHtml.Append("</div>");
}
}
return treeHtml.ToString();
}
/// <summary>
/// 取得栏目的ID
/// </summary>
private string GetMenuId(string nodeId)
{
return nodeId.Length > 0 ? nodeId : Convert.ToString(++index);
}
/// <summary>
/// 取得节点值
/// </summary>
private string GetAttributesValue(XmlAttribute attributeValue)
{
return attributeValue == null ? "" : attributeValue.Value.Trim();
}
/// <summary>
/// 是否有权限
/// </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;
}
}
}

相关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;
}

最后效果

此代码我觉得易放便扩展,只要稍改动一下代码就能使用在自己的项目中。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章