This worked example applies stylingand functionality to a basic grid of data to produce a simple formcontrol that's a pleasure to use.
This worked example applies stylingand functionality to a basic grid of data to produce a simple formcontrol that's a pleasure to use.
Features:
We want to show several data records in a handy grid, let the user sort the list, and delete one or more selected records.
We'll start with a basic table.
We'll make the first row stand out by making it a <thead> instead of <tr>. Instead of table-data cells, <td>s, table heads use <th>.
So far our table doesn't have any styling applied. Next step is to give it a CSS class that sets various properties.
.tabular_list th { border:1px solid; border-color:javascript:void(0)ddd javascript:void(0)999 javascript:void(0)888 javascript:void(0)ddd; background-color:javascript:void(0)ddd; font-weight:bold; text-align:left; color:javascript:void(0)003; padding:2px; padding-bottom:0px; font-size:.9em;}.tabular_list td { border:1px solid; border-color:javascript:void(0)fff javascript:void(0)bbb javascript:void(0)bbb javascript:void(0)fff; background-color:javascript:void(0)fff; padding:1px; font-size:.9em;} |
These CSS styling rules are contained in a separate file, interface_styles.css.
The table is now described as <table class="tabular_list"> in the HTML. It inherits the properties of the tabular_list class (classes are prefixed with a dot/period in CSS).
The CSS sets properties for all table cells (<td>s) and tableheader cells (<th>s) in any element whose class is"tabular_list".
<th>s get a 3D-effect border (top and left edges are lighterthan the background; right and bottom edges are slightly darker), aswell as padding (empty space around the contents of the cell).
<td>s get a grey right edge and bottom edge. This produces the light grid effect.
Currently, the table seems to melt into its background. To make it stand out more clearly, I'll apply a thin black border, again using CSS.
The table now sits inside a <div class="form_border">
.form_border { border:1px solid javascript:void(0)333;} |
I want users to be able to select records in the list (which might be in order to delete them, or 'mark as read').
I'll add a further column, with checkboxes in each, plus a 'select all' type checkbox in the column header.
<script type="text/javascript"><!--function checkAll(wotForm,wotState) { for (a=0; a<wotForm.elements.length; a++) { if (wotForm.elements[a].id.indexOf("delete_") == 0) { wotForm.elements[a].checked = wotState ; } }}// --></script> |
The HTML for the master 'select/deselect all' checkbox looks like this:
<input type="checkbox" onClick="checkAll(this.form,this.checked);" /> |
The call to the checkAll function passes two parameters: firstly,the current form object; followed by the checked status of the mastercheckbox after it's clicked.
The function loops through all the elements in the form objectpassed to it, and tests whether the element's name starts with thestring "delete_". (indexOf returns the first occurrence of a substring within another string, starting at position zero. If the substring is not found, -1 is returned).
If the substring is found at position zero (i.e. the name of thecheckbox begins with "delete_"), the function sets the checked propertyof the current form control to True.
We'll need a sorted-column indicator, to show which column is currently sorted by.
Each column header should be clickable, to change the sort order. So we need to apply a link anchor to column headers, enabling user to click anywhere in the <th>.
For the sorted-column indicator, I'll just use a down- or up-arrow GIF image.
Now, each table head cell needs to be totally clickable. The wayI've done this is to put an anchor/link tag (<a href="etc">) inthe <th>, and set the anchor's properties in the stylesheet asfollows.
th a { color:javascript:void(0)003; display:block; width:100%; height:100%;} |
The color setting makes the text very dark blue.
"display:block;" makes the anchor behave like a box, rather than like a wrap-around (which would be style "display:inline;". (Note: By default, <div> tags are "display:block;" , whereas <span> tags are display:inline;)
By setting its display to block, I can then set the size of the boxto width 100% and height 100%, which makes it fill the space.
(I know the CSS specification says that anchors should nevercontain block elements, but I can't find a better way to achieve thiseffect, so in this case the standard can take a back seat).
Now, if you move the mouse over any of the column head cells(except the 'select all'), the link highlights, telling you that it'sactive.
I've also used the title attribute of the anchor tag (i.e. <atitle="Link description>Link</a> to provide more informationon what clicking each column head will do.