明显的树形菜单结构。可以这样定义:
/// <summary> /// 树形菜单 /// </summary> public class MenuTreeNode { /// <summary> /// Id /// </summary> public int id { get; set; } /// <summary> /// 节点名称 /// </summary> public string text { get; set; } /// <summary> /// 图标 /// </summary> public string icon { get; set; } /// <summary> /// /// </summary> public int level { get; set; } /// <summary> /// 特定数据 /// </summary> public object tag { get; set; } /// <summary> /// 自定义属性 /// </summary> public object href { get; set; } /// <summary> /// 子节点 /// </summary> public List<MenuTreeNode> nodes { get; set; } }
至于后台加载需要用到递归,C#参考代码:
private MenuTreeNode TransformTreeNode() { MenuTreeNode menuTreeNode = new MenuTreeNode { id = this.PId, text = this.Text, icon = this.IconCls, level = 1, tag = 0, href = this.Url, nodes = new List<MenuTreeNode>() }; return menuTreeNode; } /// <summary> /// 转换成前台需要的菜单集合 /// </summary> /// <param name="list"></param> /// <returns></returns> public static List<MenuTreeNode> ConvertTreeNodes(List<B_Menu> list) { List<MenuTreeNode> nodes = new List<MenuTreeNode>(); LoadTreeNode(list, nodes, 0); return nodes; } /// <summary> /// 递归获取菜单 /// </summary> /// <param name="menuList"></param> /// <param name="menuTreeNodes"></param> /// <param name="pid"></param> private static void LoadTreeNode(List<B_Menu> menuList, List<MenuTreeNode> menuTreeNodes, int pid) { foreach (B_Menu bMenu in menuList) { if (bMenu.PId == pid) { MenuTreeNode node = bMenu.TransformTreeNode(); menuTreeNodes.Add(node); LoadTreeNode(menuList, node.nodes, node.id); } } }
请问B_Menu是什么…
@wumengchao1234: 你数据库里面的菜单表
@大师兄丶: 多谢,非常有用的思想
1 struct Level3 2 { 3 string areaName; 4 }; 5 6 struct Level2 7 { 8 string provinceName; 9 list<Level3> listAreas; 10 }; 11 12 struct Level1 13 { 14 string nationName; 15 list<Level2> listProvinces; 16 };
最后传Level1的一个list
这个结构怎么样?