spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column4 / 1 To page 1To page 2current page
[previous]

Java Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Use JavaScript to Create a Scrolling Grid

An init method is used to locate the row/column header and value <span> elements defined during the rendering phase. This is easily done thanks to the document.getElementsByTagName() method. This method gathers all the elements of a given tag name and returns them in order in an array. The column headers come first, then each row with a row header at the beginning of each row (this follows the way in which they were rendered).

A similar technique is used to find the <td> elements used for the scroll indicators; first the <div> elements are enumerated, the second one contains the vertical scroll indicator and the tenth contains the horizontal one. These <span> and <td> elements are stored in arrays held by the Grid object so they may be used by the fill method.

Grid.prototype.init = function()
{
   // if already initialized, return
   if ( this.oTable ) return;

   this.oTable = document.getElementById(this.name);

   // get all <span> elements within the table
   var aSpan = this.oTable.getElementsByTagName('span');
   var n = 0;
   // the first few are all column headers
   this.aColSpan = new Array;
   for ( var j = 0; j < this.data.nCol; j++ )
      this.aColSpan.push(aSpan[n++]);
  
   // the remainder are row headers and cells
   this.aRowSpan = new Array;
   this.aCellSpan = new Array;
   for ( var i = 0; i < this.data.nRow; i++ )
   {
      this.aRowSpan.push(aSpan[n++]);

      this.aCellSpan.push(new Array());
      for ( j = 0; j < this.data.nCol; j++ )
         this.aCellSpan[i].push(aSpan[n++]);
   }  

   // get all the <div> elements
   var aDiv = this.oTable.getElementsByTagName('div');

   // the vertical scroll is in aDiv[1]
   this.aVerticalTD = aDiv[1].getElementsByTagName('td');

   // the horizontal scroll is in aDiv[9]
   this.aHorizontalTD = aDiv[9].getElementsByTagName('td');
}

All that is left now is to demonstrate how the Grid class is used. In fact, all that is required is to instantiate a Grid object wherever it is required within the <body> of the web page, supplying a unique name and a data object with the required properties defined.

<script>
var data =
{
   nCol:6,
   nRow:10,
   nCellWidth:70,
   nCellHeight:20,
   colHeaders:["USD", "EUR","GBP", ...],
   rowHeaders:["USD", "EUR","GBP", ...],
   values:[["-",1.1,1.8,...],[...], ...]
};
new Grid('theGrid', data);
</script>

Conclusion

In this article I have outlined a simple Grid class that dynamically generates a grid with scrollbars so that large tables of data can be squeezed into a small area on a web page. This allows vital information such as row and column headers or surrounding text to be visible while still having access to the full table of data. For a complete listing of the Grid code and to view a working demonstration, click here.

About the Author

Guyon Roche is a freelance web developer in London, Great Britain. He specializes in Windows platforms with an interest in bridging the gaps between different technologies, visit www.silver-daggers.co.uk for more details. He can be reached via e-mail at guyonroche@silver-daggers.co.uk


home / programming / javascript / gr / column4 / 1 To page 1To page 2current page
[previous]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: March 27, 2003
Revised: March 17, 2004

URL: http://webreference.com/programming/javascript/gr/column4/1