PDA

View Full Version : [CLOSED] TreePanel GetNodeById method always returning non-null



bayoglu
Feb 19, 2013, 5:55 PM
Hello,

Below is a dynamic tree panel example. I already have a problem with this code in another post. This post is for the return value of GetNodeById method. I want to check whether a node with a specific ID already exists in tree panel nodes. If not, I will create one and append children. If exists, I will get it and append children. The problem is that GetNodeById never returns 'null' to indicate that a node with given ID does not exist.

Thanks.



<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
BuildMenuForUser();
}
}
protected void BuildMenuForUser()
{
Ext.Net.TreePanel menuTreePanel = new Ext.Net.TreePanel();
menuTreePanel.ID = "MenuTree";
menuTreePanel.RootVisible = false;

Ext.Net.Node menuRoot = new Ext.Net.Node();
menuRoot.NodeID = "MenuRoot";
menuRoot.Leaf = false;
menuTreePanel.SetRootNode(menuRoot);

Ext.Net.Node settingsNode = new Ext.Net.Node();
settingsNode.Text = "Settings";
settingsNode.Leaf = false;
settingsNode.NodeID = "SettingsNode";
menuTreePanel.GetRootNode().AppendChild(settingsNo de);

Ext.Net.Node setting1Node = new Ext.Net.Node();
setting1Node.Text = "Setting 01";
setting1Node.Leaf = true;
menuTreePanel.GetNodeById("SettingsNode").AppendChild(setting1Node);

Ext.Net.NodeProxy checkSubNode = menuTreePanel.GetNodeById("NonExistingNode"); //still returning Ext.Net.NodeProxy

menuTreePanel.AddTo(MainMenuPanel);
}
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Ext.NET Examples</title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server"/>
<div>
<ext:Viewport ID="VP" runat="server" Layout="BorderLayout">
<Items>
<ext:Panel Title="Menu"
ID="MainMenuPanel"
runat="server"
Region="West"
Layout="AccordionLayout"
Collapsible="true"
Split="true"
Width="200"
MinWidth="200"
MaxWidth="400">
</ext:Panel>
<ext:TabPanel Title="Center"
ID="CenterPanel"
runat="server"
Region="Center"
Layout="FitLayout"
AutoScroll="true">
</ext:TabPanel>
</Items>
</ext:Viewport>
</div>
</form>
</body>
</html>


5660

Baidaly
Feb 19, 2013, 11:29 PM
Hello!

Please, take a look at the following example:



<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
public static Node FindNodeById(TreePanelBase tree, string nodeID)
{
return FindNodeById(tree.Root, nodeID);
}

private static Node FindNodeById(NodeCollection items, string nodeID)
{
foreach (var item in items)
{
if (item.NodeID == nodeID)
return item as Node;
else
{
var node = FindNodeById((item as Node).Children, nodeID);
if (node != null)
return node;
}
}
return null;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
BuildMenuForUser();
}
}

protected void BuildMenuForUser()
{
Ext.Net.TreePanel menuTreePanel = new Ext.Net.TreePanel();
menuTreePanel.ID = "MenuTree";
menuTreePanel.RootVisible = false;

Ext.Net.Node menuRoot = new Ext.Net.Node();
menuRoot.NodeID = "MenuRoot";
menuRoot.Leaf = false;
menuRoot.Expanded = true;
menuTreePanel.Root.Add(menuRoot);

Ext.Net.Node settingsNode = new Ext.Net.Node();
settingsNode.Text = "Settings";
settingsNode.Leaf = false;
settingsNode.NodeID = "SettingsNode";
menuRoot.Children.Add(settingsNode);

Ext.Net.Node setting1Node = new Ext.Net.Node();
setting1Node.Text = "Setting 01";
setting1Node.Leaf = true;
setting1Node.EmptyChildren = true;
settingsNode.Children.Add(setting1Node);

var node = FindNodeById(menuTreePanel, "SettingsNode");
var anotherNode = FindNodeById(menuTreePanel, "NonExistingNode");

menuTreePanel.AddTo(MainMenuPanel);
}
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Ext.NET Examples</title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<div>
<ext:Viewport ID="VP" runat="server" Layout="BorderLayout">
<Items>
<ext:Panel Title="Menu" ID="MainMenuPanel" runat="server" Region="West" Layout="AccordionLayout"
Collapsible="true" Split="true" Width="200" MinWidth="200" MaxWidth="400">
</ext:Panel>
<ext:TabPanel Title="Center" ID="CenterPanel" runat="server" Region="Center" Layout="FitLayout"
AutoScroll="true">
</ext:TabPanel>
</Items>
</ext:Viewport>
</div>
</form>
</body>
</html>

Daniil
Feb 20, 2013, 3:49 AM
Hello,

Just to clarify. The GetNodeById method returns a NodeProxy instance. It doesn't retrieve an actual node. It is just useful to generate JavaScript.

Generally, this method (and the SetRootNode one) should be used during an AJAX event (DirectEvent or DirectMethod) only.

At a creating stage you can traverse Children as @Baidaly demonstrated.

bayoglu
Feb 20, 2013, 12:28 PM
Thank you @Baidaly, @Daniil. It makes sanse. Please mark as solved.

pawangyanwali
Apr 30, 2013, 4:07 AM
private static Node FindNodeById(NodeCollection items, string nodeID)
{
foreach (var item in items)
{
if (item.NodeID == nodeID)
return item as Node;
else
{
var node = FindNodeById((item as Node).Children, nodeID);
if (node != null)
return node;
}
}
return null;
}

public static Node FindNodeById(TreePanelBase tree, string nodeID)
{
return FindNodeById(tree.Root, nodeID);
}


I tried to use the above given function but it is returning null. Where did I make mistake?



string nodeId = "TEST";
var node = FindNodeById(this.GetCmp<TreePanel>("treePanel"), nodeId);
var parentNodeId = node.ParentNode.NodeID.ToString();

Daniil
Apr 30, 2013, 6:12 AM
This

this.GetCmp<TreePanel>("treePanel")
creates a TreePanel's proxy, not a real TreePanel. So, it doesn't contain any nodes.