0
我正在使用JSTree的MVC項目,我試圖將子節點添加到樹,但是我得到一個空的參考錯誤對象引用未設置爲線上的對象的實例subGroupNode.children.Add(itemNode);
我想這是因爲subGroupNode.Children
是空的。在之前的foreach循環中創建孩子的時候怎麼會這樣呢?JSTree添加節點到子節點
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'對象 –
感謝replyng尼古拉,我以爲我是在添加行'subGroupNode.children.Add(itemNode)的subChildNode;' –
因此,在使用'Add'方法(我相信數組方法)之前,您應該先創建該屬性並在其中分配一個空數組?至少這是如何在JavaScript中工作。 –