Tables and the DOM

We all know that with CSS-based layouts we aren't using tables for page design. Instead we use them for their proper purpose, data. Data tables are useful for displaying all sorts of information, but there are a number of ways we can add functionality to the tables to improve their usability.

In this tutorial we're going to look at ways manipulate tables of data using JavaScript and the W3C Document Object Model. We'll use two examples, providing highlighting for active rows and columns as we mouse-over them, and re-ordering the table based on the column we select. Examples work with IE 5.5+, Navigator 6, Firefox and Opera 7.

$2.79
- OR -

Overview

The HTML and CSS

We're going to start by building a basic HTML page, tables.htm, containing a simple table:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1"" />

<title>Tables</title>

<script type="text/javascript" src="tables.js"></script>

<link href="tables.css" rel="stylesheet" type="text/css" />

</head>

<body>

<table summary="Details of the weekly sales data" id="table1">

  <caption>

  Weekly Sales

  </caption>

  <thead>

  <tr>

    <th scope="col">Id</th>

    <th scope="col">Name</th>

    <th scope="col">Average Sales per Week </th>

    <th scope="col">Sales this Week </th>

    <th scope="col">Sales to Date </th>

  </tr>

  </thead>

  <tbody>

  <tr>

    <td>00001</td>

    <td>Robert</td>

    <td>35</td>

    <td>20</td>

    <td>800</td>

  </tr>

  <tr>

    <td>00002</td>

    <td>Kate</td>

    <td>27</td>

    <td>30</td>

    <td>600</td>

  </tr>

  <tr>

    <td>00003</td>

    <td>Martin</td>

    <td>34</td>

    <td>25</td>

    <td>340</td>

  </tr>

  <tr>

    <td>00004</td>

    <td>Anna</td>

    <td>29</td>

    <td>45</td>

    <td>120</td>

  </tr>

  </tbody>

</table>

</body>

</html>

Matt Machell

Matt MachellA man of many talents, Matt has been a web designer, technical editor, and jewellery picker. He is currently on contract for the Birmingham City University, producing pages for research centres.

He has tech-edited a dozen books on web design and development for glasshaus, Apress and Sitepoint.

He likes music with loud guitars and games with obscure rules.

His website can be found at: http://www.eclecticdreams.com

He lives in Birmingham with his girlfriend, Frances, and a horde of spider plants.

See All Postings From Matt Machell >>

Reviews

IE Script Error

June 13, 2005 by Randall Samms

I the following Internet Explorer Script Error whenever I try the "Ordering the Rows" example.  Can anyone help me out?

Error: 'childNodes[...].firstChild' is null or not an object.

I get this error inside the orderby function: "if(rows[count].childNodes[col]..." statement.

Any suggestions or help will be appreciated.

Randy

 

RE: IE Script Error

June 17, 2005 by Matt Machell
Hi, the script definitely works in IE, so I guess something has crept in as you copied and pasted from the tutorial. Have you made an alteration to the table? If nodes are null or not and object, it could mean the node (table cell in this case) you are looking to compare is missing... -Matt

You must me logged in to write a review.