【问题标题】:Arranging Items Alphabetically in Vertical Columns在垂直列中按字母顺序排列项目
【发布时间】:2015-10-21 15:28:26
【问题描述】:

我有一个单词列表,应该按字母顺序排列,但要“垂直”。
这是现在的样子:

+-----+-----+-----+------+ | AAA |血脑屏障 | CCC | DDD | +-----+-----+-----+------+ |电子电气设备 | FFF | GGG | HHH | +-----+-----+-----+------+


每个单词都嵌入在<td> 中的<table> 中,并且每个表行始终限制为4 个项目。
我怎么能垂直显示这些词,像这样:

+-----+-----+------+ | AAA |电子电气设备 |和 | +-----+-----+------+ |血脑屏障 | FFF |所以| +-----+-----+------+ | CCC | GGG |上 | +-----+-----+------+ | 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

【问题讨论】:

  • 你想要的一清二楚。您能否向我们展示您为实现这一目标所做的努力?
  • 你需要的是The Magical Mod Function(尤其是末尾的“Mod to the Rescue”)
  • @varocarbas 你说得对,我忘记了那部分。我用代码的当前状态编辑了开头的帖子。
  • 我没有看到对之前/之后的任何参考。这段代码应该做什么?必须按照第一个样本中的结构检索信息并将其转换为第二个样本。从逻辑上讲,您的代码不需要完美运行;但至少表明你自己做了一些努力。预计如何进行这种转换?您如何存储初始信息?最终版本呢?

标签: vbscript asp-classic


【解决方案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

编辑:使用数据集。

【讨论】:

    【解决方案2】:

    您无疑已经发现,问题在于 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
    

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

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多