【问题标题】:JSTree adding nodes to child nodesJSTree将节点添加到子节点
【发布时间】:2017-01-02 22:17:53
【问题描述】:

我正在将 JSTree 与 MVC 项目一起使用,并且我正在尝试将子节点添加到树中,但是我得到一个空引用错误 Object reference not set to an instance on line subGroupNode.children.Add(itemNode); 我猜这个是因为subGroupNode.Children 是空的。在前面的 foreach 循环中创建孩子时怎么可能。

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

【问题讨论】:

  • 我看不到您将children 属性添加到subGroupNode 对象的位置
  • 感谢 Nikolay 的回复,我以为我在 subGroupNode.children.Add(itemNode); 行中添加了 subChildNode
  • 那么在使用Add 方法(我相信是数组方法)之前,您不应该先创建该属性并在那里分配一个空数组吗?至少这是它在 javascript 中的工作方式。
  • 我已经设法通过创建一个新列表并尝试将子节点添加到该列表来删除空引用,但这会创建一个无限循环。我不明白为什么它会陷入无限循环。
  • 我为每个循环添加了第二个代码,现在我可以深入到第二个节点,但是当我单击以公开下一个节点时,节点消失了,我确定这是因为我并将节点和子节点设置为相同的 ID。我对 Linq 不流利,但我认为可能有一种方法可以使用 linq 正确设置 ID

标签: c# jstree asp.net-mvc-5


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多