使用 c# 的递归菜单构建器 [英] Recursive menu builder with c#

查看:60
本文介绍了使用 c# 的递归菜单构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 c# 构建递归 html 菜单.我需要的html结构如下:

Trying to build a recursive html menu using c#. The html structure I need is as follows:

<ul>
   <li>Office</li>
   <li>Home
      <ul>
         <li>Beds</li>
         <li>Desks</li>
      </ul>
   </li>
   <li>Outdoor
      <ul>
         <li>Children
            <ul>
               <li>Playsets</li>
            </ul>
         </li>
      </ul>
   </li>
</ul>

结构显然会发生变化,因为它是动态的.目前,我正在使用 HtmlGeneric 控件,即 ul、li 并添加控件,但不确定如何将其转换为有效的递归函数.

The structure could obviously change as it is dynamic. At the moment, I'm using HtmlGeneric controls, i.e. ul, li and adding the controls, but not sure how to turn this into a efficient recursive function.

推荐答案

我不确定您用于保持字符串层次结构的结构是什么,但假设您有某种方法可以根据需要获取每个字符串的子字符串(例如,您可以从家"获得床"和书桌").

I'm not sure what you're structure for keeping the string hierarchy is but let's assume you have some way of getting the child strings of each string as required (e.g. you can get 'Beds' and 'Desks' from 'Home').

首先我将标签声明为常量:

First I would declare the tags as constants:

 public const string OPEN_LIST_TAG = "<ul>";
 public const string CLOSE_LIST_TAG = "</ul>";
 public const string OPEN_LIST_ITEM_TAG = "<li>";
 public const string CLOSE_LIST_ITEM_TAG = "</li>";

然后我会使用类似字符串生成器的东西创建一个递归方法:

Then I would create a recursive method using something like a string builder:

/// <summary>
/// Adds another level of HTML list and list items to a string
/// </summary>
/// <param name="str">The string to add</param>
/// <param name="liStrings">The list of strings at this level to add</param>
/// <param name="iTabIndex">The current number of tabs indented from the left</param>
public void GenerateHTML( System.Text.StringBuilder str, List<string> liStrings, int iTabIndex) {
   //add tabs to start of string
   this.AddTabs(str, iTabIndex);

   //append opening list tag
   str.AppendLine(OPEN_LIST_TAG);

   foreach (string strParent in liStrings) {
      //add tabs for list item
      this.AddTabs(str, iTabIndex + 1);

      //if there are child strings for this string then loop through them recursively
      if (this.GetChildStrings(strParent).Count > 0) {
         str.AppendLine(OPEN_LIST_ITEM_TAG + strParent);
         GenerateHTML(str, this.GetChildStrings(strParent), iTabIndex + 2);

         //add tabs for closing list item tag
         this.AddTabs(str, iTabIndex + 1);
         str.AppendLine(CLOSE_LIST_ITEM_TAG);
      }
      else {
         //append opening and closing list item tags
         str.AppendLine(OPEN_LIST_ITEM_TAG + strParent + CLOSE_LIST_ITEM_TAG);
      }
   }

   //add tabs for closing list tag
   this.AddTabs(str, iTabIndex);
   //append closing list tag
   str.AppendLine(CLOSE_LIST_TAG);
}

并将标签添加到一个单独的方法中:

And separate out the tab adding into a separate method:

/// <summary>
/// Appends a number of tabs to the string builder
/// </summary>
/// <param name="str">The string builder to append to</param>
/// <param name="iTabIndex">The number of tabs to append to</param>
public void AddTabs(System.Text.StringBuilder str, int iTabIndex) {
   for (int i = 0; i <= iTabIndex; i++) {
      str.Append("\t");
   }
}

然后简单地使用新的字符串构建器调用 GenerateHTML,字符串的第一级和标签索引设置为 0,它应该给你你想要的.我没有包含获取子字符串的功能,因为我不确定您使用哪种结构来执行此操作 - 请告诉我,我可以调整我的解决方案.

Then simply call GenerateHTML with a new string builder, the first level of strings and the tab index set at 0 and it should give you what you want. I haven't included the functionality to get the child strings as I wasn't sure what kind of structure you are using to do that - let me know and I can adapt my solution.

希望有所帮助,丹麦人.

Hope that helps, Dane.

这篇关于使用 c# 的递归菜单构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆