ASP 輸出表格的演算法

來源:互聯網
上載者:User

用表格輸出資料庫結果是常有的事,表格有多層結構,外層是 table 再套 tr(實際還有 tbody),再套 td,各 td 之間又可能相互影響,所以研究一下 ASP 輸出表格的演算法還是比較有趣的。

table 是最外層元素,不迴圈,使用就比較簡單了,先看只有一列的表格樣本,我們以 RecordSet 對象 rs 為資料來源,產生的結果儲存在 str 中。

dim str
str = ""
do while not rs.eof
    str = str & "<tr><td>" & rs("fld") & "</td></tr>"
    rs.MoveNext
loop
if str <> "" then
    str = "<table>" & str & "</table>"
end if

一列的情況挺簡單的,毫無可圈可點之處,多列就複雜多了,這個樣本中我們以數組作為資料來源。主要考慮一行的開頭、一行的結尾、補充最後一行的列。

dim arr(3)
arr(0) = 1
arr(1) = 2
arr(2) = 3
arr(3) = 4

dim maxColsCnt '一行的最大列數
maxColsCnt = 3
dim colsCnt '總共已經產生了多少列
colsCnt = 0
dim str
str = ""
dim i
i = 0
do while i<=UBound(arr)
    if colsCnt mod maxColsCnt = 0 then
        '一行的開頭
        str = str & "<tr>"
    end if
   
    str = str & "<td>" & arr(i) & "</td>"
    colsCnt = colsCnt + 1
   
    if colsCnt mod maxColsCnt = 0 then
        '一行的結尾
        str = str & "</tr>"
    end if
   
    i = i + 1
loop

if colsCnt mod maxColsCnt <> 0 then
    '補充最後一行的列
    do while colsCnt mod maxColsCnt <> 0
        str = str & "<td> </td>"
        colsCnt = colsCnt + 1
    loop
    str = str & "</tr>"
end if

if colsCnt > 0 then
    str = "<table>" & str & "</table>"
end if

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.