为Treenode创建列表
本文关键字:列表 创建 Treenode | 更新日期: 2023-09-27 17:49:31
我知道这一定很容易,但我就是不能让它工作…我正试图通过比较来自不同行的两列并相应地将它们作为节点或叶子来为extjs树制作列表。这是我的样本数据
ListA ListB labelName
NY Parent1
NY Leaf1
HI Parent2
AK Parent3
和这是我的c#结束…所以当我匹配NY时,我应该将Parent1作为节点,将Leaf1作为叶子,而不是HI或AK。但是这样做会把我作为家长的所有数据扔给我…即使是树叶。
SqlCommand cmd = con.CreateCommand();
comd.CommandText = "SELECT * FROM myTable";
con.Open();
SqlDataReader reader = comd.ExecuteReader();
while (reader.Read())
{
City MyData = new City();
MyData.ListA = reader["ListA"].ToString().Trim();
MyData.ListB = reader["ListB"].ToString().Trim();
MyData.labelName = reader["labelName"].ToString().Trim();
giveData.Add(MyData);
}
int count = 1;
List<TreeNode> myNode = new List<TreeNode>();
foreach (City MyData in giveData)
{
// 1st foreach
if (MyData.ListA != "")
{
TreeNode treeNode = new TreeNode();
treeNode.id = count++;
treeNode.name = MyData.labelName;
treeNode.leaf = false;
List<TreeNode> Level1 = new List<TreeNode>();
foreach (City labelName in giveData)
{
if (labelName.ListA == labelName.ListB)
{// 2nd foreach
TreeNode node1 = new TreeNode();
node1.id = count++;
node1.name = labelName.labelName;
node1.leaf = true;
Level1.Add(node1);
}
}
treeNode.children = Level1;
myNode.Add(treeNode);
}
}
return JsonConvert.SerializeObject(myNode);
我应该使用数组来存储每条记录并比较它们吗?我没主意了……我相信有更好的方法来做到这一点……请帮助
假设数据是您声明的方式,并且"父"将在任何叶子之前出现,下面是我想到的创建树的单一方法:
[不相关的代码片段]
Update: LINQ using Dictionary<string, List<TreeNode>>
我创建了一个新的类TreeNode和一些用于测试的样本数据:
var MyData = new List<City>
{
new City {ListA = "AK", ListB = "", labelName = "Alaska"},
new City {ListA = "HI", ListB = "", labelName = "Hawaii"},
new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 1"},
new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 2"},
new City {ListA = "NY", ListB = "", labelName = "New York"},
new City {ListA = "", ListB = "NY", labelName = "New York Leaf 1"},
new City {ListA = "", ListB = "NY", labelName = "New York Leaf 2"}
};
这个新方法基本上创建了2个列表,1个用于父节点,1个用于叶节点。然后循环遍历叶子以找到任何匹配的父节点,并将叶子添加到其中:
var index = 0;
var parents = (from p in MyData
where p.ListB == ""
select p).ToDictionary(p => p.ListA, p => new TreeNode { id = index++, name = p.labelName, leaf = false });
var leaves = (from l in MyData
where l.ListA == ""
group l by l.ListB into stateGroup
select stateGroup).ToDictionary(g => g.Key, g => g.ToList());
foreach (var leaf in leaves.Where(leaf => parents.ContainsKey(leaf.Key)))
{
parents[leaf.Key].children =
leaf.Value.Select(l => new TreeNode {id = index++, name = l.labelName, leaf = true}).ToList();
}
var myNode = parents.Select(p => p.Value).ToList();
return JsonConvert.SerializeObject(myNode);
我认为这应该比使用列表和List.Find()
您最好的选择可能是Linq -我创建了一个快速而肮脏的VB。Net解决方案使您朝着正确的方向前进-主要部分是第二个Linq语句…
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As New List(Of City)
x.Add(New City With {.ListA = "NY", .ListB = "", .Leaf = "Parent1"})
x.Add(New City With {.ListA = "", .ListB = "NY", .Leaf = "Leaf1"})
x.Add(New City With {.ListA = "HI", .ListB = "", .Leaf = "Parent2"})
x.Add(New City With {.ListA = "AK", .ListB = "", .Leaf = "Parent3"})
tv1.Nodes.AddRange((From y In x Where y.ListA <> "" Select New TreeNode With {
.Name = y.ListA,
.Text = y.Leaf}).ToArray)
For Each nd As TreeNode In tv1.Nodes
Dim Nm As String = nd.Name
nd.Nodes.AddRange((From y In x Where y.ListB = Nm Select New TreeNode(y.Leaf)).ToArray)
Next
End Sub
简单地说,先填充父节点的第一步,然后循环遍历所有节点,并使用ListB = Node的名称填充它们。
希望这能帮助你朝着正确的方向前进
…我试着用c#写这个,但这应该可以做到:
int count = 1;
List<TreeNode> myNode = new List<TreeNode>();
foreach (City MyData in giveData)
{
// 1st foreach
if (MyData.ListA != "")
{
TreeNode treeNode = new TreeNode();
treeNode.id = count++;
treeNode.name = MyData.labelName;
treeNode.leaf = false;
foreach (City labelName in giveData)
{
if (MyData.ListA == labelName.ListB)
{// 2nd foreach
TreeNode node1 = new TreeNode();
node1.id = count++;
node1.name = labelName.labelName;
node1.leaf = true;
treeNode.Nodes.Add(node1);
}
}
myNode.Add(treeNode);
}
}
return JsonConvert.SerializeObject(myNode);