萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格跨列合併 (colspan) 實作教學:分組標題與總計列完整範例

HTML 表格跨列合併 (colspan) 實戰:分組標題、總計列與複雜報表結構

colspan (Column Span) 讓單一儲存格橫向跨越多個欄位,常用於分組標題、總計/小計列、複雜報表標頭。本文提供完整可執行範例與設計模式。

基本語法與核心邏輯

<td colspan="N">內容</td>  或  <th colspan="N">標題</th>
  • N 為正整數,表示向右合併幾個欄位 (含自己)。
  • 該儲存格**佔據 N 個欄位位置**,後續 <td>/<th> 自動順延。
  • 同一列中,所有 colspan 總和 + 未合併儲存格數 = 表格總欄數。

範例 1:分組標題 (最常見用法)

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
  <meta charset="UTF-8">
  <title>表格跨列合併 - 分組標題</title>
  <style>
    .report-table { width: 100%; border-collapse: collapse; margin: 1rem 0; font-size: 14px; }
    .report-table th, .report-table td { border: 1px solid #dee2e6; padding: 10px 12px; text-align: center; }
    .report-table thead th { background: #2c3e50; color: #fff; font-weight: 600; }
    .report-table .group-header { background: #e8f4fd; font-weight: 600; }
    .report-table .total-row { background: #fff3cd; font-weight: 700; }
  </style>
</head>
<body>
  <table class="report-table">
    <caption>2024 年各季度產品銷售報表</caption>
    <thead>
      <tr>
        <th rowspan="2">產品</th>
        <th colspan="4" class="group-header">上半年</th>
        <th colspan="4" class="group-header">下半年</th>
      </tr>
      <tr>
        <th>Q1</th><th>Q2</th><th>小計</th><th>佔比</th>
        <th>Q3</th><th>Q4</th><th>小計</th><th>佔比</th>
      </tr>
    </thead>
    <tbody>
      <tr><td>筆電</td><td>1,200</td><td>1,350</td><td>2,550</td><td>32%</td><td>1,400</td><td>1,550</td><td>2,950</td><td>35%</td></tr>
      <tr><td>手機</td><td>2,800</td><td>3,100</td><td>5,900</td><td>41%</td><td>3,200</td><td>3,400</td><td>6,600</td><td>40%</td></tr>
      <tr><td>配件</td><td>900</td><td>1,100</td><td>2,000</td><td>27%</td><td>1,000</td><td>1,200</td><td>2,200</td><td>25%</td></tr>
    </tbody>
    <tfoot>
      <tr class="total-row">
        <td>總計</td>
        <td colspan="4">上半年總計:10,450 台</td>
        <td colspan="4">下半年總計:11,750 台</td>
      </tr>
      <tr class="total-row">
        <td colspan="9">全年總計:22,200 台 (較去年成長 18%)</td>
      </tr>
    </tfoot>
  </table>
</body>
</html>

範例 2:多層級標頭 (巢狀分組)

<table class="report-table">
  <thead>
    <tr>
      <th rowspan="3">地區</th>
      <th colspan="6">2024 年銷售額 (萬元)</th>
    </tr>
    <tr>
      <th colspan="3">線上通路</th>
      <th colspan="3">實體通路</th>
    </tr>
    <tr>
      <th>官網</th><th>電商平台</th><th>社群直播</th>
      <th>直營門市</th><th>經銷商</th><th>量販店</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>北區</td><td>450</td><td>620</td><td>180</td><td>320</td><td>280</td><td>150</td></tr>
    <tr><td>中區</td><td>380</td><td>510</td><td>120</td><td>290</td><td>250</td><td>130</td></tr>
    <tr><td>南區</td><td>410</td><td>580</td><td>150</td><td>310</td><td>270</td><td>140</td></tr>
  </tbody>
</table>

範例 3:動態資料產生 colspan 表格 (JS 邏輯)

const salesData = [
  { product: '筆電', half: 'H1', q1: 1200, q2: 1350, h1Sub: 2550, h1Pct: '32%', q3: 1400, q4: 1550, h2Sub: 2950, h2Pct: '35%' },
  { product: '手機', half: 'H1', q1: 2800, q2: 3100, h1Sub: 5900, h1Pct: '41%', q3: 3200, q4: 3400, h2Sub: 6600, h2Pct: '40%' },
  { product: '配件', half: 'H1', q1: 900, q2: 1100, h1Sub: 2000, h1Pct: '27%', q3: 1000, q4: 1200, h2Sub: 2200, h2Pct: '25%' }
];

function renderTable(data) {
  const headers = [
    { label: '產品', rowspan: 2 },
    { label: '上半年', colspan: 4 },
    { label: '下半年', colspan: 4 }
  ];
  const subHeaders = ['Q1', 'Q2', '小計', '佔比', 'Q3', 'Q4', '小計', '佔比'];
  
  let html = '';
  headers.forEach(h => {
    html += `${h.label}`;
  });
  html += '';
  subHeaders.forEach(h => html += ``);
  html += '';
  
  data.forEach(row => {
    html += '';
    html += ``;
    html += ``;
    html += ``;
    html += '';
  });
  
  // 總計列
  const totals = data.reduce((acc, r) => {
    acc.q1 += r.q1; acc.q2 += r.q2; acc.h1Sub += r.h1Sub;
    acc.q3 += r.q3; acc.q4 += r.q4; acc.h2Sub += r.h2Sub;
    return acc;
  }, { q1:0, q2:0, h1Sub:0, q3:0, q4:0, h2Sub:0 });
  
  html += ``;
  html += ``;
  html += ``;
  html += ``;
  html += '
${h}
${row.product}${row.q1.toLocaleString()}${row.q2.toLocaleString()}${row.h1Sub.toLocaleString()}${row.h1Pct}${row.q3.toLocaleString()}${row.q4.toLocaleString()}${row.h2Sub.toLocaleString()}${row.h2Pct}
總計上半年:${totals.h1Sub.toLocaleString()} 台下半年:${totals.h2Sub.toLocaleString()} 台
全年總計:${(totals.h1Sub+totals.h2Sub).toLocaleString()} 台
'; return html; } document.getElementById('tableContainer').innerHTML = renderTable(salesData);

設計模式總結

模式colspan 用法適用場景無障礙重點
分組標題<th colspan="N">群組名</th>財報、儀表板、多維度報表scope="colgroup"
總計/小計列<td colspan="N">總計: ...</td>報表底部、分組間隔無需 scope,內容自述
多層標頭多列 thead 疊加 colspan/rowspan複雜交叉分析報表每層 th 正確 scope
內容跨欄<td colspan="N">說明/備註</td>備註欄、合併描述視內容決定

除錯檢查清單

  1. 每列的 colspan 總和 + 一般儲存格數 = 表格總欄數?
  2. thead 多列時,rowspan 是否正確對齊下層標頭?
  3. tbody 列數是否正確 (無多無少)?
  4. 合併儲存格是否加上 scope 屬性?
  5. CSS border-collapse: collapse 確保邊框不重疊?

掌握 colspan 設計模式,能將平面資料轉為層次分明、易讀易懂的專業報表,是資料呈現的關鍵技能。

copyright © 萬盛學電腦網 all rights reserved