在垂直列中按字母顺序排列项目

时间:2015-10-20 10:03:26

标签: vbscript asp-classic

我有一个单词列表,应该按字母顺序排列,但是" vertical"。
现在看来是这样的:

+-----+-----+-----+-----+
| AAA | BBB | CCC | DDD |
+-----+-----+-----+-----+
| EEE | FFF | GGG | HHH |
+-----+-----+-----+-----+


每个字都嵌入<td>内的<table>,并且每个表格行总是限制为4个项目。
我怎样才能垂直显示这些单词,如下所示:

+-----+-----+-----+
| AAA | EEE | and | 
+-----+-----+-----+
| BBB | FFF |  so |  
+-----+-----+-----+
| CCC | GGG |  on |  
+-----+-----+-----+
| DDD | HHH |     | 
+-----+-----+-----+


单词的数量是动态控制的,它可以在任何时候获得更多/更少。
这是一个用经典ASP开发的旧项目,但我也可以使用来自VB.NET的想法。


目前的代码如下(切入重要部分):

do until recordSet.EOF
    temphtml = temphtml & " <tr>" & vbcrlf 'this is where i collect all the <tr> and <td>
    for i = 1 to 4
        tempItem = recordSet("NameOfItem")
        temphtml = temphtml & tempbez & vbcrlf 
        recordSet.MoveNext
        if recordSet.EOF then exit for
    next
    temphtml = temphtml & " </tr>" & vbcrlf
loop

2 个答案:

答案 0 :(得分:1)

使用WebForm VB.NET。在aspx页面中添加服务器端表:

<asp:Table ID="tableWords" runat="server"></asp:Table>

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim tableRows As New List(Of TableRow)()
    Dim maxRows As Integer = 4
    Dim index As Integer = 0

    Dim recordSet As New DataSet()
    ' TODO: get the items from the database
    Dim nbItems As Integer = recordSet.Tables(0).Rows.Count

    For i As Integer = 0 To nbItems - 1
        Dim item As String = recordSet.Tables(0).Rows(i)("NameOfItem").ToString()

        If i < maxRows Then
            Dim tr As New TableRow()
            Dim td As New TableCell()
            td.Text = item
            tr.Cells.Add(td)
            tableRows.Add(tr)
        Else
            If i Mod maxRows - 1 = 0 Then
                index = 0
            End If
            Dim td As New TableCell()
            td.Text = item
            tableRows(index).Cells.Add(td)
            index += 1
        End If
    Next

    Dim cellsToAdd As Integer = maxRows - (nbItems Mod maxRows)
    Dim startIndex As Integer = maxRows - cellsToAdd

    If cellsToAdd < maxRows Then
        For i As Integer = startIndex To cellsToAdd
            Dim td As New TableCell()
            td.Text = "&nbsp;"
            tableRows(i).Cells.Add(td)
        Next
    End If

    tableItems.Rows.AddRange(tableRows.ToArray)
End Sub

编辑:使用DataSet。

答案 1 :(得分:0)

正如您无疑发现的那样,问题是html表语法是逐行的,而不是逐列的。如果你需要先做列,然后是行,那么你需要跳过一些额外的箍。

Dim row(3), counter, r
Const RowCount = 4
...
counter = 0
Do Until recordSet.EOF
    r = counter Mod RowCount '- figure out which row we're at
    row(r) = row(r) & "<td>" & recordSet("NameOfItem") & "</td>"
    counter = counter + 1
    recordSet.Movenext
Loop
If counter Mod RowCount > 0 Then '- add empty cells to any incomplete rows
    For r = Counter Mod RowCount to RowCount - 1
        row(r) = row(r) & "<td></td>"
    Next
End If
For r = 0 to RowCount - 1 '- assemble the table
    row(r) = "<tr>" & row(r) & "</tr>" & vbCrLf
    temphtml = temphtml & row(r)
Next

以上是快速而肮脏的(读取:未经测试)代码,但它应该让您走上正确的轨道。此外,如果您需要表格上的标题行,则需要以某种方式跟踪列数。