資料表結構
CREATE TABLE category (
cat_id AUTOINCREMENT PRIMARY KEY,
cat_name TEXT(100),
parent_id LONG DEFAULT 0, -- 父分類 ID,0 為頂層
cat_order INT DEFAULT 0, -- 同層排序
is_show YESNO DEFAULT TRUE, -- 是否顯示
u_id INT -- 區分類型:新聞/產品/文章
)
伺服器端遞迴函數
<%
' 全域變數
Dim catstr, depath
catstr = ""
depath = 0
'========================================================
' 遞迴輸出分類表格列
' parent_id: 父分類 ID
' stype: 分類類型 (u_id)
'========================================================
Function RenderCategory(parent_id, stype)
Dim rs, sql, cat_name, cat_id, parent_id1, m9, mgif, a, j, sql2, rs2
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT cat_name, cat_id, parent_id FROM category " & _
"WHERE parent_id = " & parent_id & " AND u_id = " & stype & " AND is_show = 1 " & _
"ORDER BY cat_order ASC"
Set rs = conn.Execute(sql)
If rs.EOF Then
' 無子分類,直接返回
Else
' 判斷顯示樣式
If depath > 2 Then
a = "none" ' 深層預設收合
mgif = "images/+.gif"
Else
a = "block" -- 淺層預設展開
mgif = "images/-.gif"
End If
j = 1
Do While Not rs.EOF
cat_name = rs("cat_name")
cat_id = rs("cat_id")
parent_id1 = rs("parent_id")
' 檢查是否有子分類
m9 = 0
sql2 = "SELECT COUNT(cat_id) AS t FROM category WHERE parent_id = " & cat_id & " AND u_id = " & stype
Set rs2 = conn.Execute(sql2)
If Not rs2.EOF Then m9 = rs2("t")
rs2.Close: Set rs2 = Nothing
' 決定圖示與預設顯示
If depath <= 2 Then
mgif = "images/-.gif"
a = "block"
Else
If m9 > 0 Then mgif = "images/+.gif" Else mgif = "images/-.gif"
If depath = 4 Then a = "block" Else a = "none"
End If
' 輸出表格列
catstr = catstr & "" & vbCrLf
catstr = catstr & " " & vbCrLf
If m9 > 0 Then
catstr = catstr & " " & vbCrLf
catstr = catstr & " | " & vbCrLf
catstr = catstr & " " & vbCrLf
Else
catstr = catstr & " " & vbCrLf
catstr = catstr & " | " & vbCrLf
catstr = catstr & " " & vbCrLf
End If
' 縮排
For i = 1 To depath
catstr = catstr & " "
Next
catstr = catstr & cat_name & " |
" & vbCrLf
' 遞迴處理子分類
If m9 > 0 Then
depath = depath + 4
RenderCategory cat_id, stype
depath = depath - 4
End If
j = j + 1
rs.MoveNext
Loop
End If
rs.Close: Set rs = Nothing
End Function
%>
前端 JavaScript 展開/收合
<script>
function fd(id, num) {
var t = document.getElementById("c" + id + "_1").style.display;
var newDisplay, newImg;
if (t == "none") {
newDisplay = "block";
newImg = "images/-.gif"; // 展開顯示減號
} else {
newDisplay = "none";
newImg = "images/+.gif"; // 收合顯示加號
}
for (var i = 1; i <= num; i++) {
var row = document.getElementById("c" + id + "_" + i);
var img = document.getElementById("d_" + id);
if (row) row.style.display = newDisplay;
if (img) img.src = newImg;
}
}
</script>
頁面調用方式
<table class="category-table">
<%
catstr = ""
depath = 0
RenderCategory 0, 1 ' 從頂層 (parent_id=0) 開始,類型 1=產品分類
Response.Write catstr
%>
</table>
CSS 樣式建議
.category-table { width: 100%; border-collapse: collapse; }
.dotted_class { border-bottom: 1px dotted #ccc; padding: 4px; }
.leftcatcss { padding-left: 10px; }
.dotted_class a { text-decoration: none; color: #333; }
.dotted_class a:hover { color: #0066cc; }
效能優化建議
- 快取分類樹:使用 Application 變數快取完整 HTML,資料異動時重建。
- 單次查詢取得所有分類,在記憶體中建立樹結構,避免遞迴查詢資料庫(N+1 問題)。
- JSON 輸出 + 前端渲染:現代架構建議 ASP 僅輸出 JSON,前端用 Vue/React 渲染樹狀元件。
注意事項
- 遞迴深度過大可能導致堆疊溢位,建議限制最大層級(如 5 層)。
depath 為全域變數控制縮排層級,多執行緒環境下需注意(ASP 單執行緒無此問題)。
- 圖片路徑
images/+.gif、images/-.gif 需實際存在。