1

我正在尝试建立一个具有递归关系的列表(HTML)。数据在数据集中,但如果更容易,可以转换为数据表。

我不知道实现这一目标的最佳选择是什么。我正在考虑使用嵌套中继器。

这是数据:

__ID__ | __NAME__  | __PARENT__     | __LEVEL__ 
1      | Patrick   |                | 1           
2      | Mark      |                | 1
3      | Scott     | 2              | 2
4      | Jason     |                | 1
5      | Julian    |                | 1
6      | John      | 6              | 2
7      | Steve     |                | 1
8      | George    | 1              | 2
9      | Robert    | 1              | 2 
10     | Rodney    | 8              | 3

这是我想要产生的输出

- Patrick [1]
  - George [8]
    - Rodney [10]
  - Robert [9]

- Mark [2]
  - Scott [3]

- Julian [5]
  - John [6]

- Jason [4]

- Steve [7]
4

2 回答 2

2

The easiest way to do this is to write a recursive method. The way it operates will depend on whether you want to have the method return the entire tree-structured list, or output the data as it reads it. If you want to output the data as you read it, your code might look something like this:

Private Sub OutputTree(data As DataTable, parentId As String, indentationLevel As Integer)
    For Each row As DataRow In GetChildRows(parentId)
        OutputRow(row, indentationLevel)
        OutputTree(data, row("ID").ToString(), indentationLevel + 1)
    Next
End Sub

The above code assumes that you also implement a method called GetChildRows which returns a list of all the rows that contain the given parent ID. It also assumes that you have a method called OutputRow which outputs the given row at the given indentation level.

Then, you could call the method like this:

OutputTree(myDataTable, nothing, 0)

If you want to build and return a tree structure, which is arguably the better approach, your code might look something like this:

Private Function BuildTreeNodes(data As DataTable, parentId As String) As List(Of MyTreeNode)
    Dim nodes As List(OfNew MyTreeNode)()
    For Each row As DataRow In GetChildRows(parentId)
        Dim node As New TreeNode()
        node.Row = row
        node.Children = BuildTreeNodes(data, row("ID").ToString())
        nodes.Add(node)
    Next
    Return node
End Sub

The above code assumes that you have defined a MyTreeNode class which would look something like this:

Public Class MyTreeNode
    Public Property Row As DataRow
    Public Property Children As List(Of MyTreeNode)
End Class

Then you could call the method like this:

Dim rootLevelNodes As List(Of MyTreeNode) = BuildTreeNodes(myDataTable, Nothing)
于 2013-07-25T13:17:16.917 回答
0

如果您__PARENT__ 的父项为空,这将起作用。

private void TopItems(DataTable Data)
{
    DataView view = new DataView(Data);
    view.RowFilter = "itemParent IS NULL";

    foreach (DataRowView row in view)
    {
        response.write("parent text: " + row["text"].ToString() + 
                 "parent id: " + row["id"].ToString();
        AddChildMenuItems(Data);
    }
}


//This code is used to recursively add child items by filtering by ParentID
private void AddChildtems(DataTable Data)
{
    DataView view = new DataView(Data);
    view.RowFilter = "itemParent=" + parentMenuItem.Value;
    foreach (DataRowView row in view)
    {
        response.write("child text: " + row["text"].ToString() + 
                 "child id: " + row["id"].ToString();
        AddChildtems(Data);
    }
}
于 2013-07-25T13:12:29.560 回答