在树结构上实现 IEnumerable
基于这些人的工作:
- http://dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/
- http://www.matthidinger.com/archive/2009/02/08 /asp.net-mvc-recursive-treeview-helper.aspx
我正在尝试实现一个 TreeView 帮助器,如下所示:
<%= Html.TreeView("records",
Library.Instance.Records,
r => r.Children,
r => r.ID) %>
树结构定义如下:
public class Tree<T> : TreeNode<T> where T : TreeNode<T>
{ }
public class TreeNode<T> : IDisposable where T : TreeNode<T>
{
public T Parent { get; set; }
public TreeNodeList<T> Children { get; set; }
}
public class TreeNodeList<T> : List<TreeNode<T>> where T : TreeNode<T>
{
public T Parent;
public T Add(T node)
{
base.Add(node);
node.Parent = (T)Parent;
return node;
}
public void Remove(T node)
{
if (node != null)
node.Parent = null;
base.Remove(node);
}
}
TreeView 帮助器具有以下签名:
public static string TreeView<T>(this HtmlHelper htmlHelper, string treeId,
IEnumerable<T> rootItems, Func<T, IEnumerable<T>> childrenProperty,
Func<T, string> itemContent, bool includeJavascript, string emptyContent)
{
...
}
因此,我需要我的 Tree 结构来实现 IEnumerable,这样我就可以将它与 TreeView 助手一起使用,这就引出了一个问题:在这种情况下,我将在哪里以及如何实现 IEnumerable?
Based on the work of these guys:
- http://dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/
- http://www.matthidinger.com/archive/2009/02/08/asp.net-mvc-recursive-treeview-helper.aspx
I am trying to implement a TreeView helper that would be used as such:
<%= Html.TreeView("records",
Library.Instance.Records,
r => r.Children,
r => r.ID) %>
And the tree structure is defined like this:
public class Tree<T> : TreeNode<T> where T : TreeNode<T>
{ }
public class TreeNode<T> : IDisposable where T : TreeNode<T>
{
public T Parent { get; set; }
public TreeNodeList<T> Children { get; set; }
}
public class TreeNodeList<T> : List<TreeNode<T>> where T : TreeNode<T>
{
public T Parent;
public T Add(T node)
{
base.Add(node);
node.Parent = (T)Parent;
return node;
}
public void Remove(T node)
{
if (node != null)
node.Parent = null;
base.Remove(node);
}
}
And the TreeView helper has this signature:
public static string TreeView<T>(this HtmlHelper htmlHelper, string treeId,
IEnumerable<T> rootItems, Func<T, IEnumerable<T>> childrenProperty,
Func<T, string> itemContent, bool includeJavascript, string emptyContent)
{
...
}
So as a result i need my Tree struture to implement IEnumerable so i can use it with the TreeView helper, and this leads to the question: where and how would i implement IEnumerable in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全理解树结构的确切细节,但这里有一个简单的实现,它采用通用的节点树并将其递归地呈现为 html 列表。
我使用的节点类相对简单,只有两个属性。
这是一些将树输出到控制台的测试代码。
这是上面代码的结果。
I don't fully understand the exact details of your tree structure, but here is a simple implementation that takes a generic tree of nodes and recursively renders it into html lists.
The node class that I'm using is relatively simple with only two properties.
Here is some test code that outputs the tree to the console.
And here are the results from the above code.