[英]treeview generation in asp.net?
I have one List type of my own struct.This is my struct. 我有自己的结构的一种列表类型。这是我的结构。
public struct outlineData
{
public string paragraphID;
public string outlineText;
public int outlineLevel;
}
and my List is 我的清单是
List<outlineData> outlinePara = new List<outlineData>();
So, i have added so many outlineData in my outlinePara List.Now, i want to create a TreeView based on the outlineData's outlineLevel. 因此,我在我的outlinePara List中添加了太多的outlineData。现在,我想基于outlineData的outlineLevel创建一个TreeView。
for example : outlineData's outlineLevel may be 0,1,2,3,1,2....0,1...0,1,1,1,1,1,2,3,2.... 例如:outlineData的outlineLevel可以是0,1,2,3,1,2 .... 0,1 ... 0,1,1,1,1,1,2,3,2 ....
So, Now i want to create a Treeview like that... 所以,现在我想创建一个像这样的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);
}
Please guide me to get a correct logic for this problem... 请指导我为这个问题找到正确的逻辑...
[EDITED TO USE System.Web.UI.WebControls.TreeView PER OP COMMENT] [编辑为使用System.Web.UI.WebControls.TreeView每个操作的注释]
How about something like the following pseudo code: 下面的伪代码怎么样:
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;
}
}
Like Antonio Bakula said, use parentParagraphID. 就像Antonio Bakula所说的那样,使用parentParagraphID。 You can populate a treeview dynamically by using a recursive function
您可以使用递归函数动态填充树视图
Change your outlineLevel with parentParagraphID. 使用parentParagraphID更改您的outlineLevel。
public struct outlineData
{
public string paragraphID;
public string outlineText;
//public int outlineLevel;
public string parentParagraphID;
}
After creating your list. 创建列表后。
List<outlineData> outlinePara = new List<outlineData>();
First insert the root TreeNode, then insert the rest. 首先插入根树节点,然后插入其余的树节点。
TreeNode rNode = new rNode();
rNode.Name = "root";
outlinePara.Add(rNode);
...
outlinePara.Add(lastNode);
After inserting all the nodes, just launch this function. 插入所有节点后,只需启动此功能。
populate(rNode, rNode.Name);
This function will create your level structure for your TreeView. 此功能将为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);
}
}
You will get a TreeView like this 您将获得像这样的TreeView
root
|-->0
| |-->1
| |-->1
| |-->2
| |-->2
| |-->2
|
|-->0
| |-->1
| ...
...
Tip 小费
In this code the ID is a string, it's much better to use something else, and it has to be unique, so you won't get a problem with your data placed in another parentNode. 在此代码中,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.