471,337 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

List

Hello,

What is the best way to create a structure (List) with parent and
child? For example:

Home
Contact
Products
Books
Magazines
Sport
Travel

Basically I want to store a menu structure into an object and then
loop through it and build the HTML.

How can I do this?

Thanks,
Miguel
Sep 10 '08 #1
3 1696
shapper <md*****@gmail.comwrote:
What is the best way to create a structure (List) with parent and
child? For example:

Home
Contact
Products
Books
Magazines
Sport
Travel

Basically I want to store a menu structure into an object and then
loop through it and build the HTML.

How can I do this?
Firstly, if you're researching this you'll want to get the right name -
this isn't a list, it's a tree.

Now, as for implementing it, assuming you want the same content type at
all locations, you could do something along the lines of:

public class TreeNode<T>
{
T content;
TreeNode<Tparent; // Will be null for the root
IList<TreeNode<T>children;
}

What API you put on top of that is up to you, of course.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #2
On Sep 10, 2:26*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
What is the best way to create a structure (List) with parent and
child? For example:
Home
* Contact
Products
* *Books
* *Magazines
* * * *Sport
* * * *Travel
Basically I want to store a menu structure into an object and then
loop through it and build the HTML.
How can I do this?

Firstly, if you're researching this you'll want to get the right name -
this isn't a list, it's a tree.

Now, as for implementing it, assuming you want the same content type at
all locations, you could do something along the lines of:

public class TreeNode<T>
{
* * T content;
* * TreeNode<Tparent; // Will be null for the root
* * IList<TreeNode<T>children;

}

What API you put on top of that is up to you, of course.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Basically, I need to render in my MVC project the following:

<ul id="Menu" class="Menu-Smog">
<li><%= Html.ActionLink("home", "Index", "Home") %></li>
<li>
<a href="#CMS">Cms</a>
<ul>
<li><%= Html.ActionLink("Create Post", "Create", "Post") %></li>
<li><%= Html.ActionLink("Delete File", "Delete", "File") %></li>
</ul>
</li>
<li><%= Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>

So basically I each Tree Node should render as follows:

1. Is Not Active
<li><a href="#CMS">Cms</a></li>

2. Is Not Active but has child items
<li>
<a href="#CMS">Cms</a>
<ul>
### Child Items Go Here ###
</ul>
</li>

3. Is Active and does not have child elements
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>

4. Is Active and has child elements:
<li>
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
<ul>
### Child Items Go Here ###
</ul>
</li>

So each Tree Node should have the following properties: ID, IsActive,
Name, Action and Controller

And the ability to check if it contains child items and if there is
loop through their child items applying the same rules ...

My problem is more with how the object should be and how to loop
through the Tree and follow this rules ...

Thanks,
Miguel
Sep 10 '08 #3
shapper <md*****@gmail.comwrote:

<snip>
Basically, I need to render in my MVC project the following:

<ul id="Menu" class="Menu-Smog">
<li><%= Html.ActionLink("home", "Index", "Home") %></li>
<li>
<a href="#CMS">Cms</a>
<ul>
<li><%= Html.ActionLink("Create Post", "Create", "Post") %></li>
<li><%= Html.ActionLink("Delete File", "Delete", "File") %></li>
</ul>
</li>
<li><%= Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>

So basically I each Tree Node should render as follows:

1. Is Not Active
<li><a href="#CMS">Cms</a></li>

2. Is Not Active but has child items
<li>
<a href="#CMS">Cms</a>
<ul>
### Child Items Go Here ###
</ul>
</li>

3. Is Active and does not have child elements
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>

4. Is Active and has child elements:
<li>
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
<ul>
### Child Items Go Here ###
</ul>
</li>

So each Tree Node should have the following properties: ID, IsActive,
Name, Action and Controller
I wouldn't put that in the TreeNode itself - I'd put that in the
Contents property (or Value, or whatever you want to call it). That way
you can build a generic TreeNode class which will be useful next time
you want a tree too. Then have a separate class for the content, and
*that* will have the ID, IsActive, Name, Action and Controller
properties.
And the ability to check if it contains child items and if there is
loop through their child items applying the same rules ...
That's pretty easy.
My problem is more with how the object should be and how to loop
through the Tree and follow this rules ...
Well, you can easily iterate through the direct children just by using
the List<T>. To iterate through *all* descendants, you probably want
something like this:

public IEnumerable<TGetDescendants()
{
yield return contents;
foreach (TreeNode<Tnode in children)
{
foreach (T element in node.GetDescendants())
{
yield return element;
}
}
}

Note that this isn't terribly efficient - but it can be optimised when
you've got everything actually working.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by massimo | last post: by
24 posts views Thread by Robin Cole | last post: by
4 posts views Thread by JS | last post: by
3 posts views Thread by chellappa | last post: by
reply views Thread by drewy2k12 | last post: by
reply views Thread by Atos | last post: by
12 posts views Thread by kalyan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.