Thuta Learning
HTML
IntermediateWeb Developmentအခြေခံ

Tables

HTMLသင်ခန်းစာ 1930 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Table ကို ဘယ်အချိန် သုံးရမလဲ (two-dimensional data) နဲ့ ဘယ်အချိန် မသုံးရဘူးလဲ (page layout) ခွဲခြားနိုင်ဖို့
  • tr, th, td ကို သုံးပြီး header cell နဲ့ data cell ကို မှန်မှန် ရေးနိုင်ဖို့
  • caption, thead, tbody, tfoot နဲ့ table ကို အပိုင်းလိုက် ဖွဲ့စည်းနိုင်ဖို့
  • colspan နဲ့ rowspan ကြောင့် row တစ်ခုမှာ cell ဘယ်နှခု လိုလဲ တွက်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

ကျောင်းက အမှတ်စာရင်း ဒါမှမဟုတ် ရထားအချိန်ဇယားကို စဉ်းစားကြည့်ပါ။ Cell တစ်ခုထဲက ဂဏန်း "85" ကို ဘယ်ကျောင်းသား (row) ရဲ့ ဘယ်ဘာသာ (column) လဲ ဆိုတာ သိမှ အဓိပ္ပာယ် ရှိပါတယ်။ ဒီလို row နဲ့ column နှစ်ဘက်လုံးကနေ အဓိပ္ပာယ် ရတဲ့ data အတွက် HTML မှာ table ကို သုံးပါတယ်။

Table ကို row (tr) တွေနဲ့ ဆောက်ပြီး row တစ်ခုစီထဲမှာ cell တွေ ထည့်ပါတယ်။ ခေါင်းစဉ် cell က th ဖြစ်ပြီး data cell က td ပါ။ Column တွေကို သီးခြား မရေးရပါဘူး — row တစ်ခုစီရဲ့ ပထမ cell တွေက ပထမ column ဖြစ်လာပါတယ်။

Problem → Why → Solution

ပြဿနာ: div တွေနဲ့ ဆွဲထားတဲ့ ဇယား

Pricing data ကို CSS grid နဲ့ div တွေ စီပြီး ဇယားလို ပုံပေါ်အောင် ဆွဲထားပါတယ်။ မျက်စိနဲ့ ကြည့်ရင် ကောင်းပေမယ့် screen reader user က "$19" ကို ကြားရတဲ့အခါ ဘယ် plan ရဲ့ ဘာဈေးလဲ မသိရပါဘူး၊ table navigation key တွေနဲ့ row/column လိုက် ရွှေ့လို့လည်း မရပါဘူး။ Data ကို spreadsheet ထဲ copy ကူးရင်လည်း စာကြောင်း တစ်ကြောင်းတည်း ဖြစ်သွားပါတယ်။

pricing.html
<div class="grid">
  <div class="head">Plan</div>  <div class="head">Price</div>
  <div>Free</div>              <div>$0</div>
  <div>Pro</div>               <div>$19</div>
</div>

table က ဘာ ထပ်ပေးလဲ

div က ဘာ အဓိပ္ပာယ်မှ မပါတဲ့ box ပါ။ table, th, td ကတော့ "ဒီ cell က ဒီ row နဲ့ ဒီ column header နဲ့ ဆိုင်တယ်" ဆိုတဲ့ ဆက်စပ်မှုကို browser ရဲ့ accessibility tree ထဲ ထည့်ပေးပါတယ်။ ဒါကြောင့် screen reader က cell ကို ဖတ်တဲ့အခါ "Price, $19" လို header နဲ့ တွဲပြီး ဖတ်နိုင်ပါတယ်။

Core concept: row တွေနဲ့ ဆောက်ပြီး header တွေနဲ့ အဓိပ္ပာယ် ပေး

Table ရဲ့ အခြေခံ အစိတ်အပိုင်း လေးခု ရှိပါတယ်။ table က ဇယားတစ်ခုလုံး၊ tr က row တစ်ခု၊ th က header cell (browser မှာ default အနေနဲ့ စာလုံးမည်းပြီး အလယ်မှာ ရှိ)၊ td က data cell ပါ။ Row တိုင်းမှာ cell အရေအတွက် တူရပါမယ် — မတူရင် column တွေ ရွှေ့ကုန်ပြီး data က မှားတဲ့ header အောက် ရောက်သွားပါတယ်။ Cell ထဲမှာ ရိုးရိုး စာသားထက် ပိုထည့်လို့ ရပါတယ် — link, button ဒါမှမဟုတ် ပုံ ထည့်နိုင်ပေမယ့် page section တစ်ခုလုံးကိုတော့ မထည့်ရပါဘူး။

Elementအဓိပ္ပာယ်
captionTable ရဲ့ ခေါင်းစဉ်၊ table ရဲ့ ပထမဆုံး child ဖြစ်ရ
theadColumn header row တွေ ပါတဲ့ အပိုင်း
tbodyData row တွေ ပါတဲ့ အပိုင်း (တစ်ခုထက် ပိုရှိနိုင်)
tfootစုစုပေါင်း၊ ပျမ်းမျှ လို အနှစ်ချုပ် row တွေ

thead, tbody, tfoot က row တွေကို အပိုင်းလိုက် အုပ်စုဖွဲ့ပါတယ်။ Table ရှည်ကို print ထုတ်တဲ့အခါ browser တွေက thead ကို စာမျက်နှာ တိုင်းမှာ ထပ်ပြနိုင်ပြီး CSS နဲ့လည်း အပိုင်းတစ်ခုချင်းကို ရွေးပြီး ပုံစံ ပေးရ လွယ်ပါတယ်။ tbody ကို မရေးခဲ့ရင်တောင် HTML parser က tr တွေကို tbody ထဲ အလိုအလျောက် ထည့်ပါတယ်၊ ဒါကြောင့် DOM ထဲမှာ table > tr ဆိုတာ မရှိဘဲ table > tbody > tr ပဲ ရှိပါတယ်။

Browser ရဲ့ default ပုံစံမှာ border မပါပါဘူး။ Border ပေးဖို့ ခေတ်ဟောင်း border="1" attribute ကို မသုံးဘဲ CSS ကို သုံးပါ — border-collapse: collapse; က cell border နှစ်ထပ်ကို တစ်ထပ်တည်း ဖြစ်စေပြီး th, td { border: 1px solid; padding: ...; } က cell တိုင်းကို မျဉ်းနဲ့ နေရာလွတ် ပေးပါတယ်။

Table က data အတွက်ပဲ ဖြစ်ပါတယ်။ Sidebar နဲ့ content ကို ဘေးချင်းကပ် ထားဖို့လို page layout အတွက် table သုံးတာက 2000 ခုနှစ်ဝန်းကျင်က နည်းဟောင်း ဖြစ်ပြီး screen reader က layout ကို data ဇယားလို ဖတ်ပြကာ ဖုန်းမှာလည်း ကျဉ်းအောင် ပြန်စီလို့ မရပါဘူး။ Layout အတွက် CSS Grid နဲ့ Flexbox ကို သုံးပါ။

မှတ်ထားရမယ့် ပုံစံ: row ကနေ cell ဆီ
  1. Data က row × column လား စစ်
  2. caption နဲ့ ဘာဇယားလဲ ပြော
  3. thead row ထဲမှာ th header တွေ ထား
  4. tbody row တိုင်းမှာ cell အရေအတွက် တူ

Browser က column ကို row တိုင်းရဲ့ cell အစဉ်ကနေ တည်ဆောက်ပါတယ်၊ ဒါကြောင့် cell ရေတွက်မှု မှန်ဖို့ အရေးကြီးပါတယ်။

အထွေထွေ ပုံစံ

html
<table>
  <caption>What this table shows</caption>
  <thead>
    <tr><th>Header A</th><th>Header B</th></tr>
  </thead>
  <tbody>
    <tr><td>Value A1</td><td>Value B1</td></tr>
  </tbody>
  <tfoot>
    <tr><td>Summary A</td><td>Summary B</td></tr>
  </tfoot>
</table>

caption က ပထမဆုံး ဖြစ်ရပြီး ပြီးမှ thead, tbody, tfoot လာပါတယ်။ Row တိုင်းမှာ column အရေအတွက်အတိုင်း cell ပါရပါမယ်။

Example: subscription plan ဇယား

ဒီ table က border ကို ခေတ်မမီတော့တဲ့ border="1" attribute အစား CSS နဲ့ ဆွဲထားပြီး caption၊ thead နဲ့ tbody အပိုင်းတွေ ပါပါတယ်၊ ဒါကြောင့် ဖွဲ့စည်းပုံနဲ့ အသွင်အပြင် နှစ်ခုလုံး ရှင်းလင်းပါတယ်။ ဘယ် cell တွေက th ဖြစ်ပြီး ဘယ်ဟာတွေက td လဲ သတိထားကြည့်ပါ။

plans.html
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #999; padding: 0.4rem 0.8rem; text-align: left; }
</style>

<table>
  <caption>Subscription plans</caption>
  <thead>
    <tr>
      <th>Plan</th>
      <th>Price per month</th>
      <th>Projects</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Free</td>
      <td>$0</td>
      <td>3</td>
    </tr>
    <tr>
      <td>Pro</td>
      <td>$19</td>
      <td>Unlimited</td>
    </tr>
  </tbody>
</table>

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. CSS borderstable { border-collapse: collapse; }

    Cell တစ်ခုစီရဲ့ border နဲ့ ဘေးက cell ရဲ့ border ကြားက 2px gap ကို ဖျက်ပြီး မျဉ်း တစ်ထပ်တည်း ဖြစ်စေပါတယ်။

  2. caption<caption>Subscription plans</caption>

    Table ရဲ့ ခေါင်းစဉ်ပါ။ Screen reader က table ထဲ ဝင်တဲ့အခါ ဒီ နာမည်ကို ကြေညာပါတယ်။ နောက် lesson မှာ ပိုအသေးစိတ် ကြည့်ပါမယ်။

  3. Header row<th>Price per month</th>

    th က column header ပါ။ "Price" တစ်လုံးတည်းထက် "Price per month" က unit ကိုပါ ပြောလို့ cell တွေထဲမှာ /month ထပ်ရေးစရာ မလိုပါဘူး။

  4. Data row<td>Unlimited</td>

    tbody ထဲက row တိုင်းမှာ td သုံးခု ရှိပြီး header သုံးခုနဲ့ တစ်ခုချင်း ကိုက်ပါတယ်။ "Unlimited" က Projects column အောက်မှာ ရှိပါတယ်။

  5. Sections<tbody>

    Data row တွေကို header row ကနေ ခွဲထားပါတယ်။ မရေးလည်း browser က ထည့်ပေးပေမယ့် thead ရှိရင် tbody ကို ကိုယ်တိုင် ရေးတာ ပိုရှင်းပါတယ်။

You should see
အပေါ်မှာ "Subscription plans" caption နဲ့ column 3 ခု၊ row 3 ခု ပါတဲ့ ဇယား ပေါ်ပါတယ်:
Plan | Price per month | Projects   (စာလုံးမည်း)
Free | $0 | 3
Pro | $19 | Unlimited
Cell တိုင်းမှာ မီးခိုးရောင် မျဉ်း တစ်ထပ်တည်းနဲ့ ဘယ်ဘက်ကပ် စာသား ရှိပါတယ်။

colspan, rowspan နဲ့ tfoot

colspan="2" က cell တစ်ခုကို column နှစ်ခုစာ ကျယ်စေပြီး rowspan="2" က row နှစ်ခုစာ မြင့်စေပါတယ်။ Span လုပ်ထားတဲ့ cell က နေရာယူထားလို့ အဲဒီ row (ဒါမှမဟုတ် အောက်က row) မှာ td ကို လျှော့ရေးရပါတယ်။ အောက်က ဥပမာမှာ "Monday" က row နှစ်ခုစာ မြင့်လို့ သူ့အောက်က CSS row မှာ cell နှစ်ခုပဲ ရှိပါတယ်၊ tfoot ရဲ့ "Total" ကလည်း column နှစ်ခုစာ ကျယ်လို့ အဲဒီ row မှာလည်း cell နှစ်ခုပဲ ရှိပါတယ်။

study-hours.html
<table>
  <caption>Study hours this week</caption>
  <thead>
    <tr><th>Day</th><th>Subject</th><th>Hours</th></tr>
  </thead>
  <tbody>
    <tr><td rowspan="2">Monday</td><td>HTML</td><td>2</td></tr>
    <tr><td>CSS</td><td>1</td></tr>
    <tr><td>Tuesday</td><td>JavaScript</td><td>3</td></tr>
  </tbody>
  <tfoot>
    <tr><th colspan="2">Total</th><td>6</td></tr>
  </tfoot>
</table>

Merge လုပ်ထားတဲ့ cell တွေကို ချွေတာပါ

Span တွေ များလာရင် screen reader က ဘယ် header က ဘယ် cell နဲ့ ဆိုင်လဲ ခန့်မှန်းရ ခက်လာပြီး ဖုန်းမှာ ပြန်စီရလည်း ခက်ပါတယ်။ ရှုပ်ထွေးတဲ့ ဇယား တစ်ခုကို ရိုးရှင်းတဲ့ ဇယား နှစ်ခု ခွဲတာက များသောအားဖြင့် ပိုကောင်းပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Admin dashboard ရဲ့ order စာရင်း၊ bank statement, pricing comparison, sports league ရဲ့ ရမှတ်ဇယား၊ API documentation ရဲ့ parameter ဇယား — ဒါတွေ အားလုံး table ရဲ့ နေရာတွေပါ။ Dashboard တွေမှာ column header ကို နှိပ်ပြီး sort လုပ်တာ၊ row ကို ရွေးတာ စတဲ့ feature တွေကို JavaScript နဲ့ ထပ်ထည့်ကြပေမယ့် အောက်ခံမှာ table, thead, tbody ကိုပဲ ထားကြပါတယ်၊ ဒါမှ accessibility ရပြီး copy-paste လုပ်ရင်လည်း spreadsheet ထဲမှာ column တွေ မှန်မှန် ဝင်ပါတယ်။

ဖုန်း screen ကျဉ်းကျဉ်းမှာ column များတဲ့ table က ဘေးကို ကျော်ထွက်တတ်ပါတယ်။ အများဆုံး သုံးတဲ့ ဖြေရှင်းနည်းက table ကို <div style="overflow-x: auto"> ထဲ ထည့်ပြီး ဇယားကိုပဲ ဘေးတိုက် scroll လုပ်ခိုင်းတာပါ၊ page တစ်ခုလုံး မ scroll ဖြစ်တော့ပါဘူး။ ဒီ wrapper ကို keyboard နဲ့ scroll လုပ်နိုင်ဖို့ tabindex="0" နဲ့ role="region", aria-label တို့ ထည့်ကြတာလည်း တွေ့ရပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Header row ကို td နဲ့ ရေးပြီး bold လုပ်တာ

<td><b>Price</b></td> က မျက်စိနဲ့ header လို ပေါ်ပေမယ့် browser အတွက် data cell ပဲ ဖြစ်လို့ screen reader က cell တွေကို header နဲ့ တွဲမဖတ်ပါဘူး။ Header တွေအတွက် th ကို သုံးပြီး ပုံစံကို CSS နဲ့ ပေးပါ။

2. colspan ပြီးနောက် cell အရေအတွက် မလျှော့တာ

Column 3 ခု ဇယားမှာ <td colspan="2"> ထည့်ပြီး td နှစ်ခု ဆက်ရေးမိရင် အဲဒီ row က column 4 ခုစာ ဖြစ်သွားပြီး ညာဘက်မှာ ထူးဆန်းတဲ့ column အပို ပေါ်လာပါတယ်။ Row တိုင်းရဲ့ span ပေါင်းက column အရေအတွက်နဲ့ ညီရပါမယ်။

3. CSS ထဲမှာ table > tr လို့ ရေးတာ

Parser က tbody ကို အလိုအလျောက် ထည့်လို့ table > tr selector က ဘာနဲ့မှ မကိုက်ပါဘူး၊ ဒါကြောင့် "CSS အလုပ်မလုပ်ဘူး" လို့ ထင်ရတတ်ပါတယ်။ tr ဒါမှမဟုတ် tbody tr ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Header ထဲမှာ unit ကို ရေးပါ (Price per month, Weight (kg))၊ ဒါမှ cell တိုင်းမှာ ထပ်ရေးစရာ မလိုပါဘူး။
  • ဂဏန်း column တွေကို CSS နဲ့ ညာဘက်ကပ် ထားပါ၊ ဒသမနေရာတွေ တစ်တန်းတည်း ဖြစ်လို့ နှိုင်းယှဉ်ရ လွယ်ပါတယ်။
  • Cell အလွတ်ကို ချန်မထားဘဲ "—" ဒါမှမဟုတ် "Not included" လို အဓိပ္ပာယ်ရှိတဲ့ စာသား ထည့်ပါ။
  • Column များတဲ့ table ကို overflow-x: auto wrapper ထဲ ထည့်ပြီး ဖုန်း အကျယ် 320px မှာ စမ်းကြည့်ပါ။

Mini challenge

Mini challenge: ကျောင်းသုံးပစ္စည်း receipt

Online shop တစ်ခုရဲ့ receipt ကို table နဲ့ ရေးပါ။ ပစ္စည်း သုံးမျိုး၊ column သုံးခုနဲ့ စုစုပေါင်း row တစ်ခု ပါရပါမယ်။

  • "Receipt" ပါတဲ့ caption နဲ့ Item, Qty, Amount column header တွေ thead ထဲမှာ ရှိရမယ်
  • tbody ထဲမှာ Notebook (2, $7.00), Pen (3, $4.50), Backpack (1, $25.00) row တွေ ရှိရမယ်
  • tfoot ထဲမှာ column နှစ်ခုစာ ကျယ်တဲ့ "Total" cell နဲ့ $36.50 ရှိရမယ်
  • Border တွေကို CSS နဲ့ ပေးရမယ်၊ border attribute မသုံးရ
receipt.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Receipt</title>
  <!-- TODO: collapsed borders and cell padding -->
</head>
<body>
  <table>
    <!-- TODO: caption -->
    <!-- TODO: thead with three th -->
    <!-- TODO: tbody with three item rows -->
    <!-- TODO: tfoot: Total spanning two columns, then the sum -->
  </table>
</body>
</html>

ရလဒ်: "Receipt — 12 May" caption အောက်မှာ Item | Qty | Amount header၊ Notebook | 2 | $7.00၊ Pen | 3 | $4.50၊ Backpack | 1 | $25.00 row တွေနဲ့ အောက်ဆုံးမှာ column နှစ်ခုစာ ကျယ်တဲ့ "Total" ဘေးမှာ $36.50 ပေါ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

tfoot row မှာ colspan="2" ပါတဲ့ cell က column နှစ်ခုစာ နေရာယူလို့ နောက်ထပ် cell တစ်ခုပဲ လိုပါတယ်။ $7.00 + $4.50 + $25.00 = $36.50 ပါ။

အဖြေ ကြည့်ရန်
receipt.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Receipt</title>
  <style>
    table { border-collapse: collapse; }
    th, td { border: 1px solid #999; padding: 0.3rem 0.7rem; }
    td:last-child { text-align: right; }
  </style>
</head>
<body>
  <table>
    <caption>Receipt — 12 May</caption>
    <thead>
      <tr><th>Item</th><th>Qty</th><th>Amount</th></tr>
    </thead>
    <tbody>
      <tr><td>Notebook</td><td>2</td><td>$7.00</td></tr>
      <tr><td>Pen</td><td>3</td><td>$4.50</td></tr>
      <tr><td>Backpack</td><td>1</td><td>$25.00</td></tr>
    </tbody>
    <tfoot>
      <tr><th colspan="2">Total</th><td>$36.50</td></tr>
    </tfoot>
  </table>
</body>
</html>

Quick Quiz 1 / 3

Column 3 ခုရှိတဲ့ ဇယားမှာ row တစ်ခုက <td colspan="2">Subtotal</td> နဲ့ စပါတယ်။ အဲဒီ row မှာ td ဘယ်နှခု ထပ်လိုလဲ။

Quick Quiz 2 / 3

<table><tr><td>A</td></tr></table> ကို browser က parse လုပ်ပြီးနောက် CSS ရဲ့ table > tr { color: red; } က ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

ဘယ် content ကို table နဲ့ ရေးသင့်လဲ။

မှတ်ထားရမယ့် အချက်များ

  • Table ကို row နဲ့ column နှစ်ဘက်လုံးကနေ အဓိပ္ပာယ် ရတဲ့ data အတွက်ပဲ သုံးပြီး layout အတွက် CSS Grid/Flexbox ကို သုံးပါ။
  • tr က row၊ th က header cell၊ td က data cell ဖြစ်ပြီး row တိုင်းရဲ့ cell (span အပါ) အရေအတွက် ညီရပါမယ်။
  • caption, thead, tbody, tfoot က table ကို နာမည်ပေးပြီး အပိုင်းလိုက် ဖွဲ့စည်းပါတယ်၊ tbody ကို parser က အမြဲ ထည့်ပါတယ်။
  • Border နဲ့ spacing ကို border attribute မဟုတ်ဘဲ CSS (border-collapse, border, padding) နဲ့ ပေးပါ။

HTML Living Standard — Tables — WHATWG

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Tables | Thuta Learning