JSTree 将节点添加到子节点 [英] JSTree adding nodes to child nodes

查看:47
本文介绍了JSTree 将节点添加到子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 MVC 项目中使用 JSTree,我试图将子节点添加到树中,但是我收到了一个空引用错误 Object reference not set to an instance of an object on the line subGroupNode.children.Add(itemNode); 我猜这是因为 subGroupNode.Children 是空的.在上一个 foreach 循环中创建子级时怎么可能.

I am using JSTree with a MVC project and I am trying to add child nodes to the tree, however I am getting a null refference error Object reference not set to an instance of an object on the line subGroupNode.children.Add(itemNode); I guess this is because the subGroupNode.Children is empty. how can it be when the child is created in the previous foreach loop.

    public JsonResult GetJsTree3Data()
    {
        var marketGroups = new List<JsTree3Node>();

        // Create our root node and ensure it is opened
        var root = new JsTree3Node()
        {
            id = Guid.NewGuid().ToString(),
            text = "Market Items",
            state = new State(true, false, false)
        };

        foreach (var group in GenerateGroups(connString))
        {
            if (group.marketParentGroup == 0)
            {
                var node = JsTree3Node.NewNode(group.id_str);
                node.text = group.name;
                node.state = new State(false, false, false);
                marketGroups.Add(node);
            }
        }

         foreach (var marketGroup in marketGroups)
        {
            foreach (var subGroup in GenerateGroups(connString))
            {
                if (subGroup.marketParentGroup.ToString() == marketGroup.id)
                {
                    var childNodes = new List<JsTree3Node>();

                    var childNode = new JsTree3Node();
                    childNode.id = subGroup.id_str;
                    childNode.text = subGroup.name;
                    childNode.state = new State(false, false, false);

                    childNodes.Add(childNode);

                    var subGroupNode = new JsTree3Node();
                    subGroupNode.id = subGroup.id_str;
                    subGroupNode.text = subGroup.name;
                    subGroupNode.state = new State(false, false, false);
                    subGroupNode.children = childNodes;

                    marketGroup.children.Add(subGroupNode);
                }
            }
        }

        return Json(root, JsonRequestBehavior.AllowGet);
    }

推荐答案

我通过创建与第一个相同的第二个列表并比较这两个列表解决了这个问题.

I solved the issue by creating a second list the same as the first and comparing those two lists.

 public JsonResult GetJsTree3Data()
    {
        var marketGroups = new List<JsTree3Node>();
        var marketSubGroups = new List<JsTree3Node>();

        foreach (var group in GenerateGroups(connString))
        {
            var node = JsTree3Node.NewNode(group.id_str);
            node.text = group.name;
            node.state = new State(false, false, false);
            node.parentId = group.marketParentGroup;
            marketSubGroups.Add(node);
        }

        // Create our root node and ensure it is opened
        var root = new JsTree3Node()
        {
            id = Guid.NewGuid().ToString(),
            text = "Market Items",
            state = new State(true, false, false)
        };

        foreach (var group in marketSubGroups)
        {
                var node = JsTree3Node.NewNode(group.id);
                node.text = group.text;
                node.state = new State(false, false, false);
                marketGroups.Add(node);
        }

        foreach (var marketGroup in marketGroups)
        {
            foreach (var subGroup in marketSubGroups)
            {
                if (subGroup.parentId.ToString() == marketGroup.id)
                {
                    var subGroupNode = new JsTree3Node();
                    subGroupNode.id = subGroup.id;
                    subGroupNode.text = subGroup.text;
                    subGroupNode.state = new State(false, false, false);
                    marketGroup.children.Add(subGroupNode);
                }
            }
        }

        root.children = marketGroups;

        return Json(root, JsonRequestBehavior.AllowGet);
    }

这篇关于JSTree 将节点添加到子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆