2015-10-20 75 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 

以上是快速和骯髒的(閱讀:未經測試)的代碼,但它應該讓你在正確的軌道上。另外,如果你需要表格中的標題行,你需要以某種方式跟蹤列數。