从数据表结果动态构建嵌套的html表

时间:2016-03-06 22:51:00

标签: html vb.net recursion datatable

我在使用vb.net代码制作嵌套表构建器时遇到问题。我有一个数据表存储所有与子父ID的内容,它需要代码循环循环并从用户输入动态构建表。不幸的是,我只能让它循环并输出所有数据(不是嵌套的)。我需要帮助“if(孩子有父母)然后制作嵌套表”并重复。下面的代码是我输出所有行的代码。它不会评估项目是否包含父项。这就是我需要帮助的地方。我试过的代码只会深入一级(不是递归)。任何帮助将不胜感激。

     Public Sub tablebuilder(ByRef tDataTable As DataTable)
         Response.Write("<table>")
         Response.Write("<tr><th><Child></th><th><Parent></th></tr>")
         For Each row As DataRow In tDataTable.Rows
             Response.Write("<tr><td>row.Item("IMA_ItemName_CHILD").ToString</td>")
            Response.Write("<td>row.Item("IMA_ItemName_PARENT").ToString</td></tr>")
         Next
         Response.Write("</table>")
    End Sub

任何帮助都会很棒。坚持了一段时间。

1 个答案:

答案 0 :(得分:0)

终于搞定了。一定是我的第100个猜测....没想到它会让我在sub本身内调用sub,但它确实有效。将表调用移到外面,但新tr位于正确的位置(嵌套)

Public Sub MakeSimpleTable(ByRef pDataTable As DataTable, ByRef viewer As String)

    Response.Write("<tr>")
    Response.Write("<th>Parent Id</th>")
    Response.Write("<th>Parent Name</th>")
    Response.Write("<th>QPA</th>")
    Response.Write("</tr>")

    Dim sortOrder As String = "IMA_ItemID_PARENT ASC"
    Dim rows() As DataRow = pDataTable.Select("IMA_ItemID_CHILD = '" & viewer & "'", sortOrder)
    Dim CP() As DataRow

    For Each row As DataRow In rows
        RowBuilder(row)
        viewer = row.Item("IMA_ItemID_PARENT").ToString
        CP = pDataTable.Select("IMA_ItemID_CHILD = '" & viewer & "'", sortOrder)
        If (CP.Length > 0) Then
            MakeSimpleTable(pDataTable, viewer)
        End If
    Next
End Sub

Public Sub RowBuilder(ByRef row As DataRow)

    Response.Write("<tr>")
    Response.Write("<td>")
    Response.Write(row.Item("IMA_ItemID_PARENT").ToString)
    Response.Write("</td>")
    Response.Write("<td>")
    Response.Write(row.Item("IMA_ItemName_PARENT").ToString)
    Response.Write("</td>")
    Response.Write("<td>")
    Response.Write(row.Item("PST_QtyPerAssy").ToString)
    Response.Write("</td>")
    Response.Write("</tr>")

End Sub

Public Sub CreateSearch()

    Dim mySQL As String = "editted"
    Dim myTable_Items As DataTable = GetData(mySQL)
    Dim viewer As String = mySearch

    viewer = mySearch

    Response.Write("You searched ")
    If Len(mySearch) > 0 Then Response.Write(mySearch) Else Response.Write("everything.")
    Response.Write("<br /><br />")
    Response.Write("<table border = '1'>")
    MakeSimpleTable(myTable_Items, viewer)
    Response.Write("</table>")

End Sub