
getPaginationStringfunction getPaginationString(page, totalitems, limit, adjacents, targetpage)
' 參數默認值
if not isnumeric(adjacents) then adjacents = 1
if not isnumeric(limit) then limit = 15
if not isnumeric(page) then page = 1
if targetpage = "" then targetpage = "/"
' 計算變量
prev = page - 1
nextPage = page + 1
lastpage = Ceil(totalitems, limit)
lpm1 = lastpage - 1
pagination = ""
if lastpage > 1 then
pagination = pagination & "<div class=""pagination"">"
' 上一頁
if page > 1 then
pagination = pagination & "<a href=""" & targetpage & "?page=" & prev & """>Prev</a>"
else
pagination = pagination & "<span class=""disabled"">Prev</span>"
end if
' 頁碼邏輯:三種情況
if lastpage < 7 + (adjacents * 2) then
' 頁數少,全部顯示
for counter = 1 to lastpage
if counter = page then
pagination = pagination & "<span class=""current"">" & counter & "</span>"
else
pagination = pagination & "<a href=""" & targetpage & "?page=" & counter & """>" & counter & "</a>"
end if
next
elseif lastpage >= 7 + (adjacents * 2) then
' 頁數多,分三種情況省略
if page < 1 + (adjacents * 3) then
' 接近開頭
for counter = 1 to (4 + (adjacents * 2)) - 1
' ... 生成頁碼 ...
next
pagination = pagination & "..."
pagination = pagination & "<a href=""" & targetpage & "?page=" & lpm1 & """>" & lpm1 & "</a>"
pagination = pagination & "<a href=""" & targetpage & "?page=" & lastpage & """>" & lastpage & "</a>"
elseif lastpage - (adjacents * 2) > page and page > (adjacents * 2) then
' 中間
pagination = pagination & "<a href=""" & targetpage & "?page=1"">1</a>"
pagination = pagination & "<a href=""" & targetpage & "?page=2"">2</a>"
pagination = pagination & "..."
for counter = (page - adjacents) to (page + adjacents)
' ... 生成當前附近頁碼 ...
next
pagination = pagination & "..."
pagination = pagination & "<a href=""" & targetpage & "?page=" & lpm1 & """>" & lpm1 & "</a>"
pagination = pagination & "<a href=""" & targetpage & "?page=" & lastpage & """>" & lastpage & "</a>"
else
' 接近結尾
pagination = pagination & "<a href=""" & targetpage & "?page=1"">1</a>"
' ... 省略 ...
end if
end if
' 下一頁
if page < lastpage then
pagination = pagination & "<a href=""" & targetpage & "?page=" & nextPage & """>Next</a>"
else
pagination = pagination & "<span class=""disabled"">Next</span>"
end if
pagination = pagination & "</div>"
end if
getPaginationString = pagination
end function
adjacents:當前頁左右顯示的頁碼數。... 省略。current、disabled 控制高亮與禁用樣式。將函數放入公共 include 文件,頁面調用 <%= getPaginationString(page, total, 15, 2, "list.asp") %> 即可輸出分頁 HTML。