2015-10-20 76 views
0

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

 
+-----+-----+-----+-----+ 
| 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 
+3

你想要的是清澈透明的。你能告诉我们你试图完成这样一个目标吗? – varocarbas

+1

你需要的是[The Magical'Mod' Function](http://www.codeguru.com/csharp/.net/net_asp/article.php/c19315/The-Magical-Mod-Function.htm)*(特别是最后一个叫做“Mod to the Rescue”的位)* – Lankymart

+0

@varocarbas你说得对,我忘了那部分。我使用代码的当前状态编辑了开场白。 – seph

回答

1

使用web窗体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 

编辑:使用的数据集。

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 

以上是快速和肮脏的(阅读:未经测试)的代码,但它应该让你在正确的轨道上。另外,如果你需要表格中的标题行,你需要以某种方式跟踪列数。