簡體   English   中英

在asp.net中生成treeview?

[英]treeview generation in asp.net?

我有自己的結構的一種列表類型。這是我的結構。

 public struct outlineData
    {
        public string paragraphID;
        public string outlineText;
        public int outlineLevel;
    }

我的清單是

List<outlineData> outlinePara = new List<outlineData>();

因此,我在我的outlinePara List中添加了太多的outlineData。現在,我想基於outlineData的outlineLevel創建一個TreeView。

例如:outlineData的outlineLevel可以是0,1,2,3,1,2 .... 0,1 ... 0,1,1,1,1,1,2,3,2 ....

所以,現在我想創建一個像這樣的Treeview ...

          0
             1
               2
                 3
             1
               2
          0
             1
          0
             1
             1
             1
             1
             1
               2
                 3
               2




TreeNode childNode;
            if (outlineParaInfo.outlineLevel == 0)
            {
                headNode = new TreeNode(outlineParaInfo.outlineText);
                TreeView11.Nodes.Add(headNode);
            }
            else if (outlineParaInfo.outlineLevel == 1)
            {
                childNode = new TreeNode(outlineParaInfo.outlineText);
                headNode.ChildNodes.Add(childNode);
            }

請指導我為這個問題找到正確的邏輯...

[編輯為使用System.Web.UI.WebControls.TreeView每個操作的注釋]

下面的偽代碼怎么樣:

int currentLevel = 0; 
TreeNode currentNode = null;
TreeNode childNode = null;

foreach(var outLineItem in outlinePara)
{
    if(outLineItem.outlineLevel == 0)
    {
            currentNode = new TreeNode(
                                outLineItem.paragraphID, outLineItem.outlineText);

            yourTreeView.Nodes.Add(currentNode);
            continue;
    }


    if(outLineItem.outlineLevel > currentLevel)
    {
        childNode = new TreeNode(
                          outLineItem.paragraphID, outLineItem.outlineText);

        currentNode.ChildNodes.Add(childNode);
        currentNode = childNode;

        currentLevel = outLineItem.outlineLevel;
        continue;
    }

    if(outLineItem.outlineLevel < currentLevel)
    {
       currentNode = currentNode.Parent;

       childNode = new TreeNode(
                          outLineItem.paragraphID, outLineItem.outlineText);

       currentNode.ChildNodes.Add(childNode);

       currentLevel = outLineItem.outlineLevel;
    }
}

就像Antonio Bakula所說的那樣,使用parentParagraphID。 您可以使用遞歸函數動態填充樹視圖

使用parentParagraphID更改您的outlineLevel。

public struct outlineData
{
    public string paragraphID;
    public string outlineText;
    //public int outlineLevel;
    public string parentParagraphID;
}

創建列表后。

List<outlineData> outlinePara = new List<outlineData>();

首先插入根樹節點,然后插入其余的樹節點。

TreeNode rNode = new rNode();
rNode.Name = "root";
outlinePara.Add(rNode);
...
outlinePara.Add(lastNode);

插入所有節點后,只需啟動此功能。

populate(rNode, rNode.Name);

此功能將為TreeView創建級別結構。

public void populate(TreeNode node, string parentParID)
{
    var newList = this.outlinePara.Where(x => x.parentParagraphID == parentParID).toList();
    foreach(var x in newList)
    {
        TreeNode child = new TreeNode();
        child.Name = paragraphID;
        child.Text = outlineText;
        populate(child, child.Name);
        node.Nodes.Add(child);
    }
}

您將獲得像這樣的TreeView

root
|-->0
|   |-->1
|   |-->1
|       |-->2
|       |-->2
|       |-->2
|
|-->0
|   |-->1
|   ...
...

小費

在此代碼中,ID是一個字符串,最好使用其他字符串,並且ID必須是唯一的,因此您不會在其他父節點中放置數據時遇到問題。

 private void generateTreeView()
        {
            int currentLevel = 0;
            TreeNode currentNode = null;
            TreeNode childNode = null;


            foreach (var outLineItem in outlineParagraph)
            {
                if (outLineItem.outlineLevel == 0)
                {
                    currentNode = new TreeNode(outLineItem.outlineText);
                    TreeView11.Nodes.Add(currentNode);

                    currentLevel = outLineItem.outlineLevel;
                    continue;
                }

                if (outLineItem.outlineLevel > currentLevel)
                {
                    childNode = new TreeNode(outLineItem.outlineText);

                    currentNode.ChildNodes.Add(childNode);
                    currentNode = childNode;

                    currentLevel = outLineItem.outlineLevel;
                    continue;
                }

                if (outLineItem.outlineLevel < currentLevel)
                {
                    //logic to find exact outlineLevel parent...
                    currentNode = findOutlineLevelParent(outLineItem.outlineLevel, currentNode);

                    if(currentNode!=null)
                        currentNode = currentNode.Parent;

                    childNode = new TreeNode(outLineItem.outlineText);
                    currentNode.ChildNodes.Add(childNode);

                    currentLevel = outLineItem.outlineLevel;
                    currentNode = childNode;
                    continue;
                }

                if (outLineItem.outlineLevel == currentLevel)
                {
                    currentNode = currentNode.Parent;
                    childNode = new TreeNode(outLineItem.outlineText);
                    currentNode.ChildNodes.Add(childNode);

                    currentLevel = outLineItem.outlineLevel;
                    currentNode = childNode;
                    continue;
                }
            }

        }//generateTreeView Ends here...

        private TreeNode findOutlineLevelParent(int targetLevel, TreeNode currentNode)
        {

            while (currentNode.Parent != null)
            {
                currentNode = currentNode.Parent;
                if (currentNode.Depth == targetLevel)
                {
                    return currentNode;
                }
            }

            return null;
        }   //findOutlineLevelParent ends here...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM