<ralphdahlgren.com>


 
Navigation
  • Home
  • Etomite CMS News
  • Code Library
    • Files
      • style.css
      • commonJScripts.js
      • contrastRows.js
  • Tutorials
  • Ralphs Rants
  • Site Map
  • Search Help
  • Login|Logout
 
Credits
 
Valid XHTML 1.1 Valid CSS! Powered By PHP Powered By MySQL
Powered By Etomite
 

contrastRows.js
Example Data
Row One row1 col1 row1 col2
Row Two row2 col1 row2 col2
Row Three row3 col1 row3 col2
Row Four row4 col1 row4 col2
Select Row Colors

contrastRows(tableClass='altRows',oddClass='odd',evenClass='even')


Odd Table Data Row
Even Table Data Row
Odd Table Data Row
Even Table Data Row

contrastRows('contrast','teal','olive')


The name of this newly developed javascript is contrastRows() which was written because there were quirks in every other javascript I tried to use to render alternating table row styles. The contrastRows() function addresses almost every one of these issues. One item that is noticably missing is the feature of being able to have more than two row color options. I consider this a minor issue and won't lose a moments sleep over this one discrepancey.

So, how does it work? Quite well, thank you! Well, alright, I'll scrap the smart comments and give an overview of what it does and how to implement the code. Unlike some other scripts that rely on an element id this script looks for table elements that possess a specific target class. Using this method eliminates the need for each table to have an id attribute and also circumvents a getElementsById() bug in IE and Opera which appears to finally be fixed in IE7. The code then gathers up a list of table rows for each target table. As the code examines each table row it only looks for table data cells. This is an intended design criteria because I don't want table header cells included in the element selection process. As table data cells are found they have the appropriate class added to provide our custom styling. The contrasting row style only toggles when a table row containing table data cells is encountered. This functionality allows table header cells to be interdispersed with table data cells indiscriminantly. The example above is a prime example of this logic in action.

The contrastRows() function can be used to style all tables on a rendered page by issuing a Javascript function call at the bottom of the page. It can also be used to selectively apply separate styles to individual tables by adding a Javascript function call immediately below each block of table markup. Only tables which contain a target tableClass within a tables open tag will be parsed.

One item worth mentioning here is that although this example code might appear as though color names can be sent, only class names are recognized. I have named the select box options to match the corresponding background colors for demonstration purposes only. I also include default .odd and .even classes in virtually all of my stylesheets which are very conveniently used by the contrastRows() function.

You will also notice that I have managed to cram a lot of functionality into a very small amount of code. I do this in distinct contrast to how other programmers write lines and lines of code that have very little actual functionality.

File: templates/mytpl/contrastRows.js
// Function: contrastRows(tableClass, oddClass, evenClass)
// Author: Ralph A. Dahlgren
// Version: 1.0 - 2008-06-04
// Purpose: displays table rows in alternating contrasting colors based on CSS
// Params:
//   tableClass - the table class name that triggers change (required)
//   oddClass - the CSS class for odd numbered rows (optional)
//   evenClass - the CSS class for even numbered rows (optional)
// Notes:
//   This function should circumvent any of IE's quirks which plague similar
//   scripts. If either oddClass or evenClass are omitted they will use default
//   classes [odd|even].
function contrastRows(tableClass, oddClass, evenClass)
{
  var rowClass = '';
  if(tableClass == undefined || tableClass == '') return;
  if(oddClass == undefined || oddClass == '') oddClass = 'odd';
  if(evenClass == undefined || evenClass == '') evenClass = 'even';
  tables = document.getElementsByTagName('table');
  for(i = 0; i < tables.length; i++)
  {
    if((' '+tables[i].className+' ').indexOf(tableClass) != -1)
    {
      trList = tables[i].getElementsByTagName('tr');
      for(j = 0; j < trList.length; j++)
      {
        tdList = trList[j].getElementsByTagName('td');
        if(tdList.length > 0)
        {
          rowClass = (rowClass == oddClass) ? evenClass : oddClass;
        }
        for(k = 0; k < tdList.length; k++)
        {
          tdList[k].className = tdList[k].className + ' ' + rowClass;
        }
      }
    }
  }
}


Powered by Etomite 1.1 (Prelude).
MySQL: 0.0053 s, 13 request(s), PHP: 0.2139 s, Total: 0.2192 s, document retrieved from database
2008-11-21 03:58 PM