List

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
shapper said:
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<T> parent; // Will be null for the root
IList<TreeNode<T>> children;
}

What API you put on top of that is up to you, of course.
 
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<T> parent; // 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 - <[email protected]>
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
 
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<T> GetDescendants()
{
yield return contents;
foreach (TreeNode<T> node 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top