2012-08-06 29 views
5

Tôi phải phát triển một phần web cho SharePoint đọc danh sách và tạo dạng xem dạng cây.Lập trình tạo một khung nhìn tre trong phần chia sẻ dựa trên một cột

xem

Cây phải được tổ chức như sau: Gốc (hoặc rễ) được tạo ra bởi một lĩnh vực lựa chọn đại diện cho một thể loại, ví dụ Drinks, các nút con là tên của các hàng chứa danh mục đó, chế độ xem dạng cây phải được tạo theo chương trình.

List: 
Title(string)  Category(Choice) 
Coke   Drinks 
Beer   Drinks 
Fish   Food 
Chips   Food 

Would produce this: 
Drinks 
    Coke 
    Beer 
Food 
    Fish 
    Chips 

mã tôi có cho đến nay

TreeView treeView; 
    TreeNode rootNode; 
    TreeNode childNode; 


    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) 
    { 
     // render the control 
     base.RenderContents(writer); 
    } 

    protected override void CreateChildControls() 
    { 
     List<TreeNode> items = new List<TreeNode>(); 

     base.CreateChildControls(); 

     // get the current site 
     using (SPSite Site = new SPSite(SPContext.Current.Site.Url + "/UberWiki")) 
     { 
      using (SPWeb currentWeb = Site.OpenWeb()) 
      { 

       // set the tree view properties 



       SPList list = currentWeb.Lists["Pages"]; 

       SPFieldChoice field = (SPFieldChoice)list.Fields["Categories"]; 

       foreach (string str in field.Choices) 
       { 
        treeView = new System.Web.UI.WebControls.TreeView(); 
        rootNode = new System.Web.UI.WebControls.TreeNode(str); 
        treeView.Nodes.Add(rootNode); 

        foreach (SPListItem rows in list.Items) 
        { 
         childNode = new System.Web.UI.WebControls.TreeNode(rows.Title); 
         treeView.Nodes.Add(childNode); 
        } 
       } 
      } 
      this.Controls.Add(treeView); 
      base.CreateChildControls(); 
     } 
    } 
+0

có thể giúp bạn tách riêng phần mã không được phép sử dụng ông webpart và tạo ra một C# Class riêng biệt cho việc tạo TreeNode ..? – MethodMan

Trả lời

0

Một giải pháp mà không cần phải làm sạch các chuỗi đa lựa chọn các giá trị

using (SPSite Site = new SPSite(SPContext.Current.Site.Url + "/UberWiki")) 
{ 
    using (SPWeb currentWeb = Site.OpenWeb()) 
    { 
     // set the tree view properties 
     SPList list = currentWeb.GetList(currentWeb.Url+"/Lists/Pages"); 

     SPFieldChoice field = (SPFieldChoice)list.Fields["Categories"]; 
     treeView = new System.Web.UI.WebControls.TreeView(); 

     // Add root nodes 
     foreach (string str in field.Choices) 
     { 
      rootNode = new System.Web.UI.WebControls.TreeNode(str); 
      treeView.Nodes.Add(rootNode);       
     } 

     // Add child nodes 
     foreach (SPListItem rows in list.Items) 
     { 
      childNode = new System.Web.UI.WebControls.TreeNode(rows["Title"].ToString()); 
      treeView.FindNode(rows["Categories"].ToString()).ChildNodes.Add(childNode); 
     } 
    } 
    this.Controls.Add(treeView); 
    base.CreateChildControls(); 
} 
3

Tìm thấy giải pháp:

using (SPSite Site = new SPSite(SPContext.Current.Site.Url + "/UberWiki")) 
         { 
          using (SPWeb currentWeb = Site.OpenWeb()) 
          { 

           SPList list = currentWeb.Lists["Pages"]; 
           SPFieldChoice field = (SPFieldChoice)list.Fields["Categories"]; 

           treeView = new System.Web.UI.WebControls.TreeView(); 

           foreach (string str in field.Choices) 
           { 

            treeNode = new System.Web.UI.WebControls.TreeNode(str); 

            foreach (SPListItem rows in list.Items) 
            { 
             SPFieldMultiChoiceValue multiChoice = new SPFieldMultiChoiceValue(Convert.ToString(rows["Wiki Categories"])); 

             string input = multiChoice.ToString(); 
//remove the ;# that comes with the multiple choiches 
             string cleanString = input.Replace(";#", ""); 

             if (cleanString == str) 
             { 
              string PageNameWithExt = rows.Name; 

              childNode = new System.Web.UI.WebControls.TreeNode(PageNameWithExt); 

              treeNode.ChildNodes.Add(childNode); 
             } 
            } 
            treeView.Nodes.Add(treeNode); 
           } 
          } 
         } 
         this.Controls.Add(treeView); 
         base.CreateChildControls(); 
        } 
Các vấn đề liên quan