By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

List

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.