在树结构上实现 IEnumerable

发布于 2024-08-15 13:01:35 字数 1793 浏览 3 评论 0原文

基于这些人的工作:

我正在尝试实现一个 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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-08-22 13:01:35

我不完全理解树结构的确切细节,但这里有一个简单的实现,它采用通用的节点树并将其递归地呈现为 html 列表。

public static string TreeView<T>(IEnumerable<T> rootItems,
                                 Func<T, IEnumerable<T>> childrenProperty,
                                 Func<T, string> itemContent)
{
    if (rootItems == null || !rootItems.Any()) return null;

    var builder = new StringBuilder();
    builder.AppendLine("<ul>");

    foreach (var item in rootItems)
    {
        builder.Append("  <li>").Append(itemContent(item)).AppendLine("</li>");
        var childContent = htmlHelper.TreeView(treeId,
                                               childrenProperty(item),
                                               childrenProperty,
                                               itemContent);

        if (childContent != null)
        {
            var indented = childContent.Replace(Environment.NewLine,
                                                Environment.NewLine + "  ");
            builder.Append("  ").AppendLine(indented);
        }
    }

    builder.Append("</ul>");
    return builder.ToString();
}

我使用的节点类相对简单,只有两个属性。

public class Node<T>
{
    public Node(T data)
    {
        Data = data;
        Children = new List<Node<T>>();
    }

    public T Data { get; private set; }
    public ICollection<Node<T>> Children { get; private set; }
}

这是一些将树输出到控制台的测试代码。

var Records = new[] {
    new Node<string>("one") {
        Children = {
            new Node<string>("one-one") {
                Children = {
                    new Node<string>("one-one-one"),
                    new Node<string>("one-one-two"),
                    new Node<string>("one-one-three")
                }
            },
            new Node<string>("one-two"),
            new Node<string>("one-three")
        }
    },
    new Node<string>("two"),
    new Node<string>("three")
};
Console.WriteLine(TreeView(Records,
                           r => r.Children,
                           r => r.Data));

这是上面代码的结果。

<ul>
  <li>one</li>
  <ul>
    <li>one-one</li>
    <ul>
      <li>one-one-one</li>
      <li>one-one-two</li>
      <li>one-one-three</li>
    </ul>
    <li>one-two</li>
    <li>one-three</li>
  </ul>
  <li>two</li>
  <li>three</li>
</ul>

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.

public static string TreeView<T>(IEnumerable<T> rootItems,
                                 Func<T, IEnumerable<T>> childrenProperty,
                                 Func<T, string> itemContent)
{
    if (rootItems == null || !rootItems.Any()) return null;

    var builder = new StringBuilder();
    builder.AppendLine("<ul>");

    foreach (var item in rootItems)
    {
        builder.Append("  <li>").Append(itemContent(item)).AppendLine("</li>");
        var childContent = htmlHelper.TreeView(treeId,
                                               childrenProperty(item),
                                               childrenProperty,
                                               itemContent);

        if (childContent != null)
        {
            var indented = childContent.Replace(Environment.NewLine,
                                                Environment.NewLine + "  ");
            builder.Append("  ").AppendLine(indented);
        }
    }

    builder.Append("</ul>");
    return builder.ToString();
}

The node class that I'm using is relatively simple with only two properties.

public class Node<T>
{
    public Node(T data)
    {
        Data = data;
        Children = new List<Node<T>>();
    }

    public T Data { get; private set; }
    public ICollection<Node<T>> Children { get; private set; }
}

Here is some test code that outputs the tree to the console.

var Records = new[] {
    new Node<string>("one") {
        Children = {
            new Node<string>("one-one") {
                Children = {
                    new Node<string>("one-one-one"),
                    new Node<string>("one-one-two"),
                    new Node<string>("one-one-three")
                }
            },
            new Node<string>("one-two"),
            new Node<string>("one-three")
        }
    },
    new Node<string>("two"),
    new Node<string>("three")
};
Console.WriteLine(TreeView(Records,
                           r => r.Children,
                           r => r.Data));

And here are the results from the above code.

<ul>
  <li>one</li>
  <ul>
    <li>one-one</li>
    <ul>
      <li>one-one-one</li>
      <li>one-one-two</li>
      <li>one-one-three</li>
    </ul>
    <li>one-two</li>
    <li>one-three</li>
  </ul>
  <li>two</li>
  <li>three</li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文