I'm creating a custom section with a custom tree in Umbraco 7.6
I have managed to create the nodes I want to see, however the child nodes are not showing up under the correct parent node, they are all showing up in the root.
How do you tell umbraco to render the node in the correct place?
When using the CreateTreeNode() to create the nodes one of the paras is parentId I'm pretty sure I am setting this correctly but yet the nodes still all show up on one level.
Any ideas?
This is what I have got so far.
if (id == Constants.System.Root.ToInvariantString())
{
var nodes = new TreeNodeCollection();
foreach (var opco in opCoService.GetAll(c => c.Brands))
{
var node = CreateTreeNode(opco.Id.ToString(), "-1", queryStrings, opco.CompanyName, "icon-folder", false);
nodes.Add(node);
if (opco.Brands.Any())
{
foreach (var brand in opco.Brands)
{
var childNode = CreateTreeNode(brand.Id.ToString(), opco.Id.ToString(), queryStrings, brand.BrandName);
nodes.Add(childNode);
}
}
}
return nodes;
}
The root ID is always "-1" (that's the Constants.System.Root).
If you open a sub-tree, it isn't already available in the backoffice but is requested via an async request (just look in the dev tools).
The request calls your GetTreeNodes() method with the id parameter you have clicked on.
So every time GetTreeNodes() is called, you can only return ONE level of hierarchy. That's why your CreateTreeNodes() aren't creating a nested tree.
What you have to do is the following. Divide your method into ID groups, so you can return the correct trees.
I have altered your code like this:
protected override TreeNodeCollection GetTreeNodes(string id, System.Net.Http.Formatting.FormDataCollection queryStrings)
{
var nodes = new TreeNodeCollection();
// render top-level tree (for id = -1)
if (id == Constants.System.Root.ToInvariantString())
{
foreach (var opco in opCoService.GetAll(c => c.Brands))
{
var node = CreateTreeNode(opco.Id.ToString(), Constants.System.Root.ToString(), queryStrings, opco.CompanyName, "icon-folder", false);
nodes.Add(node);
}
}
// render sub-tree if id != -1
else
{
// this line is pseudo-code, as I don't know how your service looks like
// but you'll get the idea ;-)
var opco = opCoService.GetById(id);
if (opco != null && opco.Brands.Any())
{
foreach (var brand in opco.Brands)
{
var childNode = CreateTreeNode(brand.Id.ToString(), opco.Id.ToString(), queryStrings, brand.BrandName);
nodes.Add(childNode);
}
}
}
return nodes;
}
If you have more than two levels in your tree you could (just an idea) also add a prefix to the id, like so:
How to render Child node in custom tree
I'm creating a custom section with a custom tree in Umbraco 7.6
I have managed to create the nodes I want to see, however the child nodes are not showing up under the correct parent node, they are all showing up in the root.
How do you tell umbraco to render the node in the correct place?
When using the CreateTreeNode() to create the nodes one of the paras is parentId I'm pretty sure I am setting this correctly but yet the nodes still all show up on one level.
Any ideas? This is what I have got so far.
Hi.
The root ID is always "-1" (that's the
Constants.System.Root
).If you open a sub-tree, it isn't already available in the backoffice but is requested via an async request (just look in the dev tools).
The request calls your
GetTreeNodes()
method with theid
parameter you have clicked on.So every time
GetTreeNodes()
is called, you can only return ONE level of hierarchy. That's why yourCreateTreeNodes()
aren't creating a nested tree.What you have to do is the following. Divide your method into ID groups, so you can return the correct trees.
I have altered your code like this:
If you have more than two levels in your tree you could (just an idea) also add a prefix to the id, like so:
This will give you the ability to check if the ID is from a brand, opco and so on.
I didn't actually realise that thats how it works.... I have a pretty complex tree to build this was just a test... good thing I know this now!
I have got it working now, thanks.
is working on a reply...