尝试使用递归子项使哈希集动态化以填充 Mudblazor 的 Treeview

发布于 2025-01-20 18:11:48 字数 2019 浏览 0 评论 0原文

我有一个问题,试图填充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));
        }
    }

}

现在问题是,儿童群体被添加为主要群体本身,我不确定如何递归将孩子添加到标签中。我试图使它看起来像一个文件资源管理器,但是它只是组和“设备”,而不是文件。

我一直在试图将其围绕它缠绕,大约一个星期,找不到工作解决方案。

usblazor mudtree显示这些项目全部是主要

组从Mudblazor的TreeView数据数据。所需的结果将是这样的:

图像显示带有递归儿童的树视图

这是一个图像数据库中的组是什么:

图像显示与父组ID的数据库关系

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-01-27 18:11:48

我无法制定您从数据库(无组织列表)构建树的父母优先策略。如果您不打算让数据库以合适的方式订购数据(例如,每个父母在孩子之前都会遇到每个父母),则需要处理客户端的“确保父母存在” 。将所有节点加载到可以查找它们的内存中,然后再次跑到它们之间的连接时,可能是最简单的。

var map = new Dictionary<Guid, TreeItemData>();

//make a root note (the data in the db doesn't contain it)
map[Guid.Empty] = new TreeItemData("Root", Icons.Filled.Computer){ IsExpanded = true, TreeItems = new() };
TreeItems.Add(seen[Guid.Empty]);

var nodes = get_nodes_from_db....

//create a map that knows about every node in the data and maps to a TreeItemData
foreach(var node in nodes)
    map[node.GroupId] = new TreeItemData(node.Name, Icons.Filled.Computer){ IsExpanded = true, TreeItems = new() };

//now we know about every node, we can link parents to children
foreach(var node in nodes)
    map[node.ParentGroupGroupId].TreeItems.Add(map[node.GroupId]);

第一个循环通过其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.

var map = new Dictionary<Guid, TreeItemData>();

//make a root note (the data in the db doesn't contain it)
map[Guid.Empty] = new TreeItemData("Root", Icons.Filled.Computer){ IsExpanded = true, TreeItems = new() };
TreeItems.Add(seen[Guid.Empty]);

var nodes = get_nodes_from_db....

//create a map that knows about every node in the data and maps to a TreeItemData
foreach(var node in nodes)
    map[node.GroupId] = new TreeItemData(node.Name, Icons.Filled.Computer){ IsExpanded = true, TreeItems = new() };

//now we know about every node, we can link parents to children
foreach(var node in nodes)
    map[node.ParentGroupGroupId].TreeItems.Add(map[node.GroupId]);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文