JSTree adding nodes to child nodes

无人久伴 提交于 2019-12-20 04:59:10

问题


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);
    }


回答1:


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);
    }


来源:https://stackoverflow.com/questions/41433933/jstree-adding-nodes-to-child-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!