spacer

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

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

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

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

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