2010-08-05 64 views
0

是否可以使用遞歸輸出以下HTML無序列表。C#使用遞歸從列表創建HTML無序列表

<ul> 
    <li>1 
     <ul> 
      <li>5 
       <ul> 
        <li>8</li> 
        <li>9</li> 
       </ul> 
      </li> 
      <li>6</li> 
     </ul> 
    </li> 
    <li>2</li> 
    <li>3</li> 
    <li>4</li> 
    <li>7</li> 
</ul> 

當原始數據在下面的列表

 List<Page> pages = new List<Page>(); 

     pages.Add(new Page { Id = 1, pageId = 1, parentPageId = 0 }); 
     pages.Add(new Page { Id = 2, pageId = 2, parentPageId = 0 }); 
     pages.Add(new Page { Id = 3, pageId = 3, parentPageId = 0 }); 
     pages.Add(new Page { Id = 4, pageId = 4, parentPageId = 0 }); 
     pages.Add(new Page { Id = 5, pageId = 5, parentPageId = 1 }); 
     pages.Add(new Page { Id = 6, pageId = 6, parentPageId = 1 }); 
     pages.Add(new Page { Id = 7, pageId = 7, parentPageId = 0 }); 
     pages.Add(new Page { Id = 8, pageId = 8, parentPageId = 5 }); 
     pages.Add(new Page { Id = 9, pageId = 9, parentPageId = 5 }); 

其中頁是

public class Page 
    { 
     public int Id { get; set; } 
     public int pageId { get; set; } 
     public int parentPageId { get; set; } 
    } 

在這裏舉行是我到目前爲止這當然完全不起作用。 ..

 foreach (var page in pages) 
     { 
      Console.WriteLine("<ul><li>{0}", page.pageId); 
      WriteMenu(pages, page.pageId, page.parentPageId); 
     } 


    public static void WriteMenu(List<Page> pages, int pageId, int parentPageId) 
    { 
     foreach (var page in pages) 
     { 
      if (page.parentPageId == pageId) 
      { 
       Console.WriteLine("<ul><li>{0}", page.pageId); 
       WriteMenu(pages, page.pageId, page.parentPageId); 
      } 
     } 
    } 

回答

3

H流量約:

public static bool WriteMenu(List<Page> pages, int parentId, int indent) 
{ 
    string padding = new string(' ', indent * 8); 
    bool writtenAny = false; 
    foreach (var page in pages.Where(p => p.ParentPageId == parentId)) 
    { 
     if (!writtenAny) 
     {     
      Console.WriteLine(); 
      Console.WriteLine(padding + "<ul>"); 
      writtenAny = true; 
     } 
     Console.Write(padding + " <li>{0}", page.PageId); 
     if (WriteMenu(pages, page.PageId, indent + 1)) 
     { 
      Console.WriteLine(padding + " </li>");     
     } 
     else 
     { 
      Console.WriteLine("</li>"); 
     } 
    }   
    if (writtenAny) 
    { 
     Console.WriteLine(padding + "</ul>"); 
    } 
    return writtenAny; 
}  
... 
WriteMenu(pages, 0, 0); 
+0

其近有...的2,3,4,7部分不像我的例子。 – 2010-08-05 16:42:16

+0

@Nicholas:好的,編輯 - 我相信現在看起來*完全像你的例子 - 除了你的例子由於某種原因而忽略了第6項;) – 2010-08-05 16:57:44

+0

非常好,謝謝,在那裏有Hexakosioihexekontahexaphobia的回合...對不起! – 2010-08-05 17:21:17

1

對不起,錯過了我的第一個嘗試遞歸位,這應該得到你想要的結果:

 Console.WriteLine("<ul>"); 
     foreach (var page in pages) 
     { 
      if (page.parentPageId == 0) 
      { 
       WriteMenu(pages, page); 
      } 
     } 
     Console.WriteLine("</ul>"); 



    private static void WriteMenu(List<Page> pages, Page page) 
    { 
     Console.WriteLine("<li>" + page.pageId); 

     var subpages = pages.Where(p => p.parentPageId == page.pageId); 

     if (subpages.Count() > 0) 
     { 
      Console.WriteLine("<ul>"); 
      foreach (Page p in subpages) 
      { 
       if (pages.Count(x => x.parentPageId == p.pageId) > 0) 
        WriteMenu(pages, p); 
       else 
        Console.WriteLine(string.Format("<li>{0}</li>", p.pageId)); 
      } 
      Console.WriteLine("</ul>"); 
     } 

     Console.WriteLine("</li>"); 
    } 
+0

@Nicholas不客氣,很高興幫助:D – heisenberg 2010-08-05 18:03:37