8: ടേബിളുകൾ (Tables)

കഴിഞ്ഞ അധ്യായത്തിൽ വിവരങ്ങൾ list ആയി അവതരിപ്പിക്കാൻ <ul>, <ol>, <dl> എന്നിവ പഠിച്ചു. എന്നാൽ ചില വിവരങ്ങൾ rows, columns എന്ന രീതിയിൽ പരസ്പരം താരതമ്യം ചെയ്ത് കാണിക്കേണ്ടി വരും. ഉദാഹരണത്തിന് വിദ്യാർത്ഥികളുടെ marks, product prices, course details, timetable തുടങ്ങിയവ. ഇത്തരം structured data അവതരിപ്പിക്കാൻ HTML-ൽ Tables ഉപയോഗിക്കാം.

HTML Table എന്താണ്?

Rows-ലും columns-ലും ക്രമീകരിച്ചിരിക്കുന്ന data ആണ് Table. ഒരു table-ൽ ഓരോ row-യും element ഉപയോഗിച്ചും ഓരോ data cell-ഉം <td> element ഉപയോഗിച്ചും നിർമ്മിക്കുന്നു.

<table>
  <tr>
    <td>അനു</td>
    <td>85</td>
  </tr>
  <tr>
    <td>അരുൺ</td>
    <td>92</td>
  </tr>
</table>

മുകളിലെ table-ൽ രണ്ട് rows ഉണ്ട്. ഓരോ row-ലും രണ്ട് cells വീതം ഉണ്ട്.

Table-ന്റെ പ്രധാന Elements

, , ഉപയോഗിക്കാം. ഇതിലൂടെ table-ന്റെ HTML structure കൂടുതൽ വ്യക്തവും maintainable-ഉം ആകുന്നു.

Elementപങ്ക്
മുഴുവൻ table-ന്റെ container
ഒരു table row
Table heading cell
Table data cell
Table-ന്റെ title അല്ലെങ്കിൽ caption
Table heading rows-ന്റെ group
Table-ന്റെ main data rows
Table-ന്റെ summary/footer rows

Table Row — <tr>

ഒരു table-ലെ ഓരോ horizontal row-യും <tr> element ഉപയോഗിച്ചാണ് നിർമ്മിക്കുന്നത്. tr എന്നത് Table Row എന്നാണ്.

<table>
  <tr>
    <td>HTML</td>
    <td>CSS</td>
    <td>JavaScript</td>
  </tr>
  <tr>
    <td>Structure</td>
    <td>Styling</td>
    <td>Behavior</td>
  </tr>
</table>

Table Header — <th>

ഒരു table-ലെ column അല്ലെങ്കിൽ row heading നൽകാൻ

element ഉപയോഗിക്കുന്നു. സാധാരണയായി browser heading cells-നെ visually കൂടുതൽ prominent ആയി കാണിക്കും. എന്നാൽ ഇതിന്റെ പ്രധാന ഉദ്ദേശ്യം visual appearance മാത്രമല്ല; table-ന്റെ structure മനസ്സിലാക്കാനും accessibility-ക്കും ഇത് പ്രധാനമാണ്.

<table>
  <tr>
    <th>പേര്</th>
    <th>മാർക്ക്</th>
  </tr>
  <tr>
    <td>അനു</td>
    <td>85</td>
  </tr>
  <tr>
    <td>അരുൺ</td>
    <td>92</td>
  </tr>
</table>

ഇവിടെ പേര്, മാർക്ക് എന്നിവ column headings ആണ്. അതിനാൽ അവയ്ക്ക്

ഉപയോഗിക്കുന്നു. അനു, 85 തുടങ്ങിയവ actual data ആയതിനാൽ ഉപയോഗിക്കുന്നു.

Table Data — <td>

<td> ഒരു table-ലെ സാധാരണ data cell ആണ്. td എന്നത് Table Data എന്നാണ്.

<table>
  <tr>
    <th>Subject</th>
    <th>Mark</th>
  </tr>
  <tr>
    <td>Malayalam</td>
    <td>90</td>
  </tr>
  <tr>
    <td>English</td>
    <td>82</td>
  </tr>
</table>

ഒരു Basic Table നിർമ്മിക്കാം

ഇപ്പോൾ headings-ഉം data-യും ഉൾപ്പെടുന്ന ഒരു ചെറിയ student marks table നിർമ്മിക്കാം.

<h2>Student Marks</h2>

<table>
  <tr>
    <th>Student</th>
    <th>Malayalam</th>
    <th>English</th>
    <th>Maths</th>
  </tr>
  <tr>
    <td>Anu</td>
    <td>90</td>
    <td>82</td>
    <td>88</td>
  </tr>
  <tr>
    <td>Arun</td>
    <td>85</td>
    <td>91</td>
    <td>86</td>
  </tr>
</table>

caption — Table-ന് പേര് നൽകുക

ഒരു table എന്തിനെക്കുറിച്ചാണ് എന്ന് വ്യക്തമായി പറയാൻ

element ഉപയോഗിക്കാം. ഇത് table-ന്റെ title പോലെ പ്രവർത്തിക്കുന്നു.

<table>
  <caption>Student Marks</caption>
  <tr>
    <th>Student</th>
    <th>Mark</th>
  </tr>
  <tr>
    <td>Anu</td>
    <td>90</td>
  </tr>
  <tr>
    <td>Arun</td>
    <td>85</td>
  </tr>
</table>

Table-ന്റെ ഉദ്ദേശ്യം വ്യക്തമായി മനസ്സിലാക്കാൻ caption സഹായിക്കുന്നു. Accessibility കണക്കിലെടുത്തും ഇത് പ്രയോജനകരമാണ്.

thead, tbody, tfoot

വലിയ table-കളിൽ rows-നെ logical groups ആയി വിഭജിക്കാൻ

Elementഉപയോഗം
<thead>Heading rows
<tbody>Main data rows
<tfoot>Summary അല്ലെങ്കിൽ total rows
<table>
  <caption>Student Marks</caption>

  <thead>
    <tr>
      <th>Student</th>
      <th>Mark</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Anu</td>
      <td>90</td>
    </tr>
    <tr>
      <td>Arun</td>
      <td>85</td>
    </tr>
  </tbody>

  <tfoot>
    <tr>
      <td>Average</td>
      <td>87.5</td>
    </tr>
  </tfoot>
</table>

ഇവിടെ <thead> heading row-നും <tbody> student data-ക്കും <tfoot> average പോലെയുള്ള summary information-നും ഉപയോഗിച്ചിരിക്കുന്നു.

colspan — ഒന്നിലധികം Columns ഒന്നാക്കുക

ഒരു cell ഒന്നിലധികം columns-ന്റെ സ്ഥലം എടുക്കേണ്ടപ്പോൾ colspan attribute ഉപയോഗിക്കാം. ഉദാഹരണത്തിന് മൂന്ന് subjects-ന്റെ മുകളിൽ ഒരൊറ്റ heading നൽകണമെങ്കിൽ colspan ഉപയോഗിക്കാം.

<table>
  <tr>
    <th>Name</th>
    <th colspan="3">Marks</th>
  </tr>
  <tr>
    <td>Anu</td>
    <td>90</td>
    <td>82</td>
    <td>88</td>
  </tr>
</table>

colspan="3" എന്നത് ആ cell മൂന്ന് columns-ന്റെ വീതി ഉൾക്കൊള്ളണം എന്നാണ് അർത്ഥമാക്കുന്നത്.

rowspan — ഒന്നിലധികം Rows ഒന്നാക്കുക

ഒരു cell ഒന്നിലധികം rows-ന്റെ സ്ഥലം എടുക്കേണ്ടപ്പോൾ rowspan ഉപയോഗിക്കാം.

<table>
  <tr>
    <th>Student</th>
    <th>Subject</th>
    <th>Mark</th>
  </tr>
  <tr>
    <td rowspan="2">Anu</td>
    <td>Malayalam</td>
    <td>90</td>
  </tr>
  <tr>
    <td>English</td>
    <td>85</td>
  </tr>
</table>

ഇവിടെ Anu എന്ന cell രണ്ട് rows-ന്റെ height ഉൾക്കൊള്ളുന്നു. അതിനാലാണ് rowspan="2" ഉപയോഗിച്ചിരിക്കുന്നത്.

colspan-ഉം rowspan-ഉം ഒരുമിച്ച്

കൂടുതൽ complex tables-ൽ colspan, rowspan എന്നിവ ഒരുമിച്ച് ഉപയോഗിക്കാം. പക്ഷേ table structure കൃത്യമായി ആസൂത്രണം ചെയ്യണം. ഓരോ row-യിലും എത്ര columns ഉണ്ടെന്ന് മനസ്സിലാക്കി മാത്രമേ ഇവ ഉപയോഗിക്കാവൂ.

<table>
  <tr>
    <th colspan="2">Course Details</th>
  </tr>
  <tr>
    <th>Course</th>
    <th>Duration</th>
  </tr>
  <tr>
    <td>HTML</td>
    <td>4 Weeks</td>
  </tr>
  <tr>
    <td>CSS</td>
    <td>5 Weeks</td>
  </tr>
</table>

Table-ൽ Headers ശരിയായി നൽകുന്നത്

ഒരു table-ൽ data cells മാത്രം ഉപയോഗിച്ച് headings എഴുതുന്നത് ഒഴിവാക്കുക. Column അല്ലെങ്കിൽ row-നെ തിരിച്ചറിയാൻ സഹായിക്കുന്ന heading-കൾക്ക് ഉപയോഗിക്കുക. ഇത് table കാണുന്ന ഉപയോക്താവിനും screen reader ഉപയോഗിക്കുന്ന ഉപയോക്താവിനും data-യുടെ ബന്ധം മനസ്സിലാക്കാൻ സഹായിക്കും.

<table>
  <caption>Course Fees</caption>
  <thead>
    <tr>
      <th>Course</th>
      <th>Duration</th>
      <th>Fee</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>HTML</td>
      <td>4 Weeks</td>
      <td>₹2000</td>
    </tr>
    <tr>
      <td>CSS</td>
      <td>5 Weeks</td>
      <td>₹2500</td>
    </tr>
  </tbody>
</table>

Table Accessibility

Tables data അവതരിപ്പിക്കാനുള്ള semantic structure ആണ്. Accessibility-ക്കായി table-ന് meaningful caption നൽകുകയും heading cells-ന് ഉപയോഗിക്കുകയും ചെയ്യുന്നത് പ്രധാനമാണ്.

കൂടുതൽ complex tables-ൽ headings ഏത് data-യുമായി ബന്ധപ്പെട്ടതാണ് എന്ന് കൂടുതൽ വ്യക്തമായി വ്യക്തമാക്കേണ്ടി വരാം. സാധാരണ tables-ൽ <th>, <caption&tg;, ശരിയായ row/column structure എന്നിവ ഉപയോഗിക്കുന്നത് നല്ല തുടക്കമാണ്.

Table എന്തിനാണ് ഉപയോഗിക്കേണ്ടത്?

Table ഉപയോഗിക്കേണ്ടത് tabular data അവതരിപ്പിക്കാനാണ്. ഉദാഹരണത്തിന് marks, prices, schedules, statistics, comparison data എന്നിവയ്ക്ക് table അനുയോജ്യമാണ്.

Layout നിർമ്മിക്കാൻ Table ഉപയോഗിക്കരുത്

പഴയ websites-ൽ page layout നിർമ്മിക്കാൻ tables വ്യാപകമായി ഉപയോഗിച്ചിരുന്നു. Modern HTML-ൽ webpage layout നിർമ്മിക്കാൻ tables ഉപയോഗിക്കുന്നത് നല്ല രീതിയല്ല. Layout-നായി CSS-ലെ Flexbox, Grid തുടങ്ങിയ techniques ആണ് പിന്നീട് പഠിക്കുക. Table data അവതരിപ്പിക്കാൻ മാത്രം ഉപയോഗിക്കുക.

<!-- Table should contain data, not the whole page layout -->
<table>
  <tr>
    <td>Left side of webpage</td>
    <td>Right side of webpage</td>
  </tr>
</table>

Table-ന്റെ Content ക്രമീകരിക്കുന്നത്

ഒരു table എഴുതുന്നതിന് മുമ്പ് അതിലെ columns എന്തൊക്കെയാണ്, rows എന്തൊക്കെയാണ്, heading ഏതാണ്, summary ആവശ്യമുണ്ടോ എന്നിവ തീരുമാനിക്കുന്നത് നല്ല ശീലമാണ്. ആദ്യം data structure ആലോചിച്ച ശേഷം HTML എഴുതുക.

  • ആദ്യം table-ന്റെ purpose തീരുമാനിക്കുക.
  • Columns-ന് meaningful headings നൽകുക.
  • Data rows തിരിച്ചറിയുക.
  • ആവശ്യമെങ്കിൽ caption നൽകുക.
  • വലിയ table ആണെങ്കിൽ thead, tbody, tfoot ഉപയോഗിക്കുക.
  • Cells merge ചെയ്യേണ്ടപ്പോൾ മാത്രം colspan അല്ലെങ്കിൽ rowspan ഉപയോഗിക്കുക.
  • Table page layout-നായി ഉപയോഗിക്കരുത്.

ഒരു Practical Example — Course Schedule

ഇപ്പോൾ ഒരു course schedule table നിർമ്മിക്കാം. ഇതിൽ caption, thead, tbody, th, td എന്നിവ ഒരുമിച്ച് ഉപയോഗിക്കുന്നു.

<!DOCTYPE html>
<html lang="ml">
<head>
  <meta charset="UTF-8">
  <title>Course Schedule</title>
</head>
<body>

  <h1>Web Development Course</h1>

  <table>
    <caption>Weekly Course Schedule</caption>

    <thead>
      <tr>
        <th>Day</th>
        <th>Topic</th>
        <th>Duration</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>Monday</td>
        <td>HTML Basics</td>
        <td>2 Hours</td>
      </tr>
      <tr>
        <td>Wednesday</td>
        <td>Links and Images</td>
        <td>2 Hours</td>
      </tr>
      <tr>
        <td>Friday</td>
        <td>Lists and Tables</td>
        <td>2 Hours</td>
      </tr>
    </tbody>

  </table>

</body>
</html>

Browser-ൽ Table എങ്ങനെ കാണാം?

HTML table-ന് CSS styling ഒന്നും നൽകിയിട്ടില്ലെങ്കിൽ browser അതിന്റെ default appearance ഉപയോഗിച്ചാണ് table കാണിക്കുക. Borders, spacing, colors, alignment തുടങ്ങിയവ CSS ഉപയോഗിച്ച് പിന്നീട് നിയന്ത്രിക്കാം. ഇപ്പോൾ നമ്മുടെ ശ്രദ്ധ table-ന്റെ HTML structure-ലാണ്.

Common Beginner Mistakes

  • tr>, <th>, <td> എന്നിവയുടെ nesting തെറ്റായി എഴുതുക.
  • Table heading-കൾക്ക് <td> ഉപയോഗിച്ച് semantic meaning നഷ്ടപ്പെടുത്തുക.
  • caption ആവശ്യമുള്ള table-ലും caption ഒഴിവാക്കുക.
  • colspan അല്ലെങ്കിൽ rowspan value തെറ്റായി നൽകുക.
  • Table-നെ webpage layout നിർമ്മിക്കാൻ ഉപയോഗിക്കുക.
  • Table data-യുടെ columns തമ്മിലുള്ള ബന്ധം വ്യക്തമല്ലാത്ത രീതിയിൽ structure ചെയ്യുക.
  • വലിയ table-ൽ thead, tbody, tfoot എന്നിവ ഉപയോഗിക്കാതെ structure ബുദ്ധിമുട്ടാക്കുക.
  • CSS ഇല്ലാത്ത default table appearance-നെ final design ആയി കരുതുക.

ഈ അധ്യായത്തിൽ പഠിച്ച പ്രധാന കാര്യങ്ങൾ

  • <table> ഉപയോഗിച്ച് table നിർമ്മിക്കാം.
  • <tr> ഒരു table row-നെ പ്രതിനിധീകരിക്കുന്നു.
  • <th> heading cell-നും data cell-നും ഉപയോഗിക്കുന്നു.
  • <caption> table-ന്റെ title അല്ലെങ്കിൽ description നൽകുന്നു.
  • <thead>, <tbody>, <tfoot> ഉപയോഗിച്ച് table structure group ചെയ്യാം.
  • colspan ഉപയോഗിച്ച് columns merge ചെയ്യാം.
  • rowspan ഉപയോഗിച്ച് rows merge ചെയ്യാം.
  • Meaningful headings, caption എന്നിവ accessibility-ക്ക് സഹായിക്കുന്നു.
  • Tables tabular data-യ്ക്ക് ഉപയോഗിക്കണം; webpage layout-ന് ഉപയോഗിക്കരുത്.
  • Table styling പിന്നീട് CSS ഉപയോഗിച്ച് ചെയ്യാം.

പരിശീലനങ്ങൾ

  • നിങ്ങളുടെ class-ലെ അഞ്ച് വിദ്യാർത്ഥികളുടെ പേര്, സ്ഥലം, mark എന്നിവ ഉൾപ്പെടുന്ന ഒരു table നിർമ്മിക്കുക.
  • ഒരു weekly timetable table നിർമ്മിക്കുക.
  • Course name, duration, fee എന്നിവ ഉൾപ്പെടുന്ന ഒരു course table നിർമ്മിക്കുക.
  • caption, thead, tbody എന്നിവ ഉപയോഗിച്ച് ഒരു table നിർമ്മിക്കുക.
  • colspan ഉപയോഗിച്ച് മൂന്ന് columns-ന്റെ മുകളിൽ ഒരു common heading നൽകുക.
  • rowspan ഉപയോഗിച്ച് ഒരേ category-യിലുള്ള രണ്ട് rows group ചെയ്യുക.
  • ഒരു table data-യും ഒരു webpage layout-ഉം തമ്മിലുള്ള വ്യത്യാസം code ഉപയോഗിച്ച് മനസ്സിലാക്കുക.

Quiz & Self Assessment

ചോദ്യം 1 / 4
ഒരു HTML table-ൽ ഒരു row നിർമ്മിക്കാൻ ഏത് element ആണ് ഉപയോഗിക്കുന്നത്?

ഇതോടെ HTML-ന്റെ അടിസ്ഥാന content elements ആയ headings, paragraphs, text formatting, links, images, lists, tables എന്നിവ നമ്മൾ പഠിച്ചു. അടുത്ത ഘട്ടത്തിൽ webpage-ൽ ഉപയോക്താക്കളിൽ നിന്ന് വിവരങ്ങൾ സ്വീകരിക്കാൻ ഉപയോഗിക്കുന്ന Forms പഠിക്കാൻ തുടങ്ങാം.