PageAdmin网站内容管理系统(CMS)交流论坛

注册

 

QQ登录

只需一步,快速开始

发新话题 回复该主题

导航菜单自定义函数问题 [复制链接]

1#
  1.     private string ColumnNav(int parentId=0,int levelId=1)
  2.     {
  3.         
  4.         
  5.         int siteId = Html.CurrentSiteId();//获取当前站点Id
  6.         IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
  7.         columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
  8.         columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
  9.         if (columnList.Count()==0)
  10.         {
  11.             return ""; //没有子集则返回空字符串
  12.         }
  13.         if (levelId == 1) {
  14.             string html = "<ul class=\"nav-list\">";
  15.         }else {
  16.             string html = "<ul class=\"menu\">";
  17.         }
  18.         
  19.         foreach (dynamic item in columnList)
  20.         {
  21.             int columnId = item.Id;//调用栏目id字段
  22.             string name = item.Name; //调用栏目Name字段
  23.             string url = Html.ColumnUrl(columnId);//获取栏目url地址
  24.             int level = item.Level;//调用栏目level字段
  25.             //自定义拼接开始
  26.             html += "<li>";
  27.             html += "<a href=\"" + url + "\">" + name  + "</a>";
  28.             html += ColumnNav(columnId,level);  //递归获取
  29.             html += "</li>";
  30.         }
  31.         html += "</ul>";
  32.         return html;
  33.     }
复制代码
上述代码是自定义导航css样式属性值,根据level值的去判断是否是二级菜单。但是,前端都是levelId == 1这条语句的代码块。

通过以下两个函数,就可以实现,但是两个函数的代码重复率很高,能不能在一个函数实现。
  1. private string ColumnNav(int parentId=0)
  2.     {
  3.         
  4.         
  5.         int siteId = Html.CurrentSiteId();//获取当前站点Id
  6.         IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
  7.         columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
  8.         columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
  9.         if (columnList.Count()==0)
  10.         {
  11.             return ""; //没有子集则返回空字符串
  12.         }
  13.         string html = "<ul class=\"nav-list\">";
  14.         
  15.         foreach (dynamic item in columnList)
  16.         {
  17.             int columnId = item.Id;//调用栏目id字段
  18.             string name = item.Name; //调用栏目Name字段
  19.             string url = Html.ColumnUrl(columnId);//获取栏目url地址
  20.             int level = item.Level;//调用栏目level字段
  21.             //自定义拼接开始
  22.             html += "<li>";
  23.             html += "<a href=\"" + url + "\">" + name  + "</a>";
  24.             html += SecondaryMenu(columnId);  //递归获取
  25.             html += "</li>";
  26.         }
  27.         html += "</ul>";
  28.         return html;
  29.     }

  30.     private string SecondaryMenu(int parentId)
  31.     {
  32.         int siteId = Html.CurrentSiteId();//获取当前站点Id
  33.         IEnumerable<dynamic> columnList=Html.GetColumnList(c => c.SiteId == siteId);//获取当前站点的所有栏目数据集
  34.         columnList = columnList.Where(c => c.ParentId == parentId);//筛选当前级别的数据
  35.         columnList = columnList.OrderBy(c => c.Xuhao);//按序号排序
  36.         if (columnList.Count()==0)
  37.         {
  38.             return ""; //没有子集则返回空字符串
  39.         }
  40.         string html = "<ul class=\"menu\">";
  41.         foreach (dynamic item in columnList)
  42.         {
  43.             int columnId = item.Id;//调用栏目id字段
  44.             string name = item.Name; //调用栏目Name字段
  45.             string url = Html.ColumnUrl(columnId);//获取栏目url地址
  46.             int level = item.Level;//调用栏目level字段
  47.             //自定义拼接开始
  48.             html += "<li>";
  49.             html += "<a href=\"" + url + "\">" + name  + "</a>";
  50.             html += "</li>";
  51.         }
  52.         html += "</ul>";
  53.         return html;
  54.     }
复制代码
分享 转发
TOP
2#

level为什么需要加1
TOP
发新话题 回复该主题