Roll your own datagrid with prototype (part 1 the table)
May 27th, 2008Today I’m going to start a three part tutorial in implementing a custom datagrid with prototype & scriptaculous. In part 1 we’ll explore the html & css underlying the widget. Parts 2 & 3 will look at interactivity and legacy browser support (IE 6).
Disclaimer: this project is designed to work on IE 7 and later. It will not render properly on IE 6. We will look into that more with part 3.
So it starts with a table. Tables are much maligned nowadays. The focus is on semantic markup and creative uses of <div> and <span>. In the energy behind css layouts over the past few years, the new markup elements available to the venerable table have been slightly overlooked. Sure the reign of table based layouts is over, but tables are now able to display data more semantically and style it more simply than ever before.
Here’s a traditional HTML table to start from.
| Item Number | Description | Qty | Unit Price | Total Price |
| 0023 | Apples | 3 | .56 | 1.68 |
| 0057 | Pears | 13 | .74 | 9.62 |
| 0137 | Bananas | 8 | .31 | 2.48 |
| 0587 | Kumquats | 17 | .26 | 4.42 |
| 0054 | Oranges | 3 | .45 | 1.35 |
| Total | 44 | 19.55 |
<table>
<tr><td> Item Number</td><td>Description</td><td>Qty</td><td>Unit Price</td><td>Total Price</td></tr>
<tr><td>0023</td><td> Apples</td><td>3</td><td>.56</td><td>1.68</td></tr>
<tr><td>0057</td><td> Pears</td><td>13</td><td>.74</td><td>9.62</td></tr>
<tr><td>0137</td><td> Bananas</td><td>8</td><td>.31</td><td>2.48</td></tr>
<tr><td>0587</td><td> Kumquats</td><td>17</td><td>.26</td><td>4.42</td></tr>
<tr><td>0054</td><td> Oranges</td><td>3</td><td>.45</td><td>1.35</td></tr>
<tr><td></td><td>Total</td><td>44</td><td></td><td>19.55</td></tr>
</table>
It’s a decent start, but we can do better. First lets separate the body from the head with <thead> & <tbody> tags. We’ll also replace the header cells with <th> tags to denote their importance. We’re also going to add a set of row numbers. We’ll use <th> for these as well to signify that they aren’t really part of the data (note the empty <th/> we added to the header to make everything stay consistent).
<table>
<thead>
<tr><th /><th> Item Number</th><th>Description</th><th>Qty</th><th>Unit Price</th><th>Total Price</th></tr>
</thead><tbody>
<tr><th>1</th><td>0023</td><td> Apples</td><td>3</td><td>.56</td><td>1.68</td></tr>
<tr><th>2</th><td>0057</td><td> Pears</td><td>13</td><td>.74</td><td>9.62</td></tr>
<tr><th>3</th><td>0137</td><td> Bananas</td><td>8</td><td>.31</td><td>2.48</td></tr>
<tr><th>4</th><td>0587</td><td> Kumquats</td><td>17</td><td>.26</td><td>4.42</td></tr>
<tr><th>5</th><td>0054</td><td> Oranges</td><td>3</td><td>.45</td><td>1.35</td></tr>
<tr><td></td><td>Total</td><td>44</td><td></td><td>19.55</td></tr>
</tbody></table>
The <thead> specifies the header content. When printed, a browser should print it on every page the table spans. More importantly for us, it is a unique dom node we can grab for styling instructions. <tbody> works in a similar way, It allows us to break the internals of a table into a set of segments. This will come in handy later. <th> is a different beast entirely. I like to think of it as the ying to <td>’s yang. <th> & <td> are to <table> what <dt> & <dd> are to <dl>: a semantic way to denote name / value pairs.
Now that our table has a header and a body, It’s time to give it a footer. We’ll do this by using the <tfoot> tag.
At first glance, there is visually no difference between the 2nd and 3rd examples. Both of them look identical. The source, tells a different story. In the 3rd example, the footer row is actually ahead of the <tbody> in the source. This is the beauty of <tfoot> It lets us place the footer early in the table and cleanly separate it from the tbody that follows. It also guarantees that the <tfoot> appears last no matter what we append to the table via script.
All of this looks good, except it would be great if the columns were better styled. The two columns with financial data especially should be aligned left to ensure that the decimal points align. In a perfect world, we’d be able to do this with column groups, but that’s something for part 2.






