尝试使用递归子项使哈希集动态化以填充 Mudblazor 的 Treeview
我有一个问题,试图填充Mudblazor的泥泞。
我当前的代码是:
protected override async Task OnInitializedAsync()
{
Devices = await httpClient.GetFromJsonAsync<List<Device>>("https://localhost:8443/api/device");
DeviceGroups = await httpClient.GetFromJsonAsync<List<DeviceGroup>>("https://localhost:8443/api/group");
TreeItemData rootTreeItem = new TreeItemData("Root", Icons.Filled.Computer)
{
IsExpanded = true
};
TreeItems.Add(rootTreeItem);
rootTreeItem.TreeItems = new HashSet<TreeItemData>() { };
foreach (var group in DeviceGroups)
{
if (group.ChildGroups != null)
{
foreach (DeviceGroup childgroup in group.ChildGroups)
{
rootTreeItem.TreeItems.Add(new TreeItemData(group.Name, Icons.Filled.Computer)
{
IsExpanded = true,
TreeItems = new HashSet<TreeItemData>()
{
new TreeItemData(childgroup.Name, Icons.Filled.Computer)
{
IsExpanded = true,
}
}
});
}
}
else if (group.ParentGroup != null)
{
}
else
{
rootTreeItem.TreeItems.Add(new TreeItemData(group.Name, Icons.Filled.Computer));
}
}
}
现在问题是,儿童群体被添加为主要群体本身,我不确定如何递归将孩子添加到标签中。我试图使它看起来像一个文件资源管理器,但是它只是组和“设备”,而不是文件。
我一直在试图将其围绕它缠绕,大约一个星期,找不到工作解决方案。
组从Mudblazor的TreeView数据数据。所需的结果将是这样的:
这是一个图像数据库中的组是什么:
I have a problem trying to fill a Mudtree from Mudblazor.
My current code is this:
protected override async Task OnInitializedAsync()
{
Devices = await httpClient.GetFromJsonAsync<List<Device>>("https://localhost:8443/api/device");
DeviceGroups = await httpClient.GetFromJsonAsync<List<DeviceGroup>>("https://localhost:8443/api/group");
TreeItemData rootTreeItem = new TreeItemData("Root", Icons.Filled.Computer)
{
IsExpanded = true
};
TreeItems.Add(rootTreeItem);
rootTreeItem.TreeItems = new HashSet<TreeItemData>() { };
foreach (var group in DeviceGroups)
{
if (group.ChildGroups != null)
{
foreach (DeviceGroup childgroup in group.ChildGroups)
{
rootTreeItem.TreeItems.Add(new TreeItemData(group.Name, Icons.Filled.Computer)
{
IsExpanded = true,
TreeItems = new HashSet<TreeItemData>()
{
new TreeItemData(childgroup.Name, Icons.Filled.Computer)
{
IsExpanded = true,
}
}
});
}
}
else if (group.ParentGroup != null)
{
}
else
{
rootTreeItem.TreeItems.Add(new TreeItemData(group.Name, Icons.Filled.Computer));
}
}
}
Now what the problem is that the child groups get added as main groups themselves, I am not sure how to recursively add children to a hashset. I am trying to have it look like a file explorer basically, but instead of files, it is simply groups and "devices".
I have been trying to wrap my head around it for around a week now and could not find a working solution.
Mudblazor Mudtree showing the items all being main groups
I have to use a hashset because I am binding the data to a Treeview, from Mudblazor. The desired result would be something like this:
Image showing the Treeview with recursive children
Here is an image of what the groups are like in the database:
Image showing the database relations with the parent group IDs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法制定您从数据库(无组织列表)构建树的父母优先策略。如果您不打算让数据库以合适的方式订购数据(例如,每个父母在孩子之前都会遇到每个父母),则需要处理客户端的“确保父母存在” 。将所有节点加载到可以查找它们的内存中,然后再次跑到它们之间的连接时,可能是最简单的。
第一个循环通过其ID创建每个节点。然后,可以确保第二个循环在地图中确实存在任何给定的parentid,因此它可以直接访问它,并
在本质上添加当前的treeItem作为该父treeTem的孩子,
map
是索引从groupId到treeitemdata,因此可以轻松地说“查找这个父X,然后将这个孩子y添加到X的孩子中”https://try.mudblazor.com/snippet/qecgeylqqgshnoul
I couldn't work out your parent-first strategy for building your tree from your database (an unorganized list). If you're not going to get the database to order your data in a suitable fashion (so that every parent is encountered before it's children, for example) you'll need to handle a "ensure parent exists before child" in the client side. It might be simplest to load all your nodes into a memory that can look them up, then run over them again making the connections between nodes.
The first loop creates every node by its ID. The second loop can then be assured that any given ParentId does exist in the map so it can just straight up access it, and add the current TreeItem as a child of that parent treeitem
In essence
map
is an index from GroupId to a TreeItemData, so it makes it easy to say "lookup this parent X, then add this child Y to X's children"https://try.mudblazor.com/snippet/QEcGEylQQgshnOUL