spacer

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

home / programming / javascript / gr / column4 / 1 current pageTo page 2To page 3
[next]

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

A problem often encountered in web design is condensing large tables of data into a standard 800x600 web page. If the table is too big, the user will need to scroll the browser window to see all of the data, which means that the surrounding text and any row or header columns cannot be seen.

If the table is either very wide or very tall, this problem can be easily averted by a crafty use of the overflow style as in the example below…

Fig 1. An example of Vertical or Horizontal scrolling using the overflow style

For tables that are both wide and tall, this technique won’t work, as it’s not possible to synchronize all of the scrollbars. Here, we need to use a different technique. In this article I show how JavaScript can be used to implement a grid with vertical and horizontal scroll bars so that any size of table data may be rendered in a small grid.

Fig 2. The scrolling grid. NB: The values in this table bear no relation to any currency, real or imaginary.

The scrolling grid is managed by a single JavaScript class called “Grid”. The constructor function of the Grid class will render the HTML grid like the one shown above but first it must perform a few initialization steps…

First, the two arguments of the Grid constructor are stored for later use. The name argument is a unique name used for storing the Grid object as a property of the window object. The data argument contains information needed by the Grid including row and column headers, cell size, and more…

function Grid(name, data)
{
   this.name = name;
   this.data = data;
 
   var cursor = document.all ? "hand" : "pointer";
   window[name] = this;
 
   this.xOffset = 0;
   this.yOffset = 0;

Now the rendering process begins. Rather than calling document.write() multiple times (which can be slow), I’ve stored the HTML text in an array which will be combined into one string at the end of the rendering. First, a <table> element is added with an id that matches the name of the Grid object…

   // render the grid
   var aStr = new Array();
   aStr.push('<table border=1 cellpadding=0 cellspacing=0 id="' + name + '">');

The first row contains the column headers. Note that each column header is placed inside a <span> element inside a <th>. The reason for this will become apparent when using the init method.

   aStr.push('<tr>');
   aStr.push('<td>&nbsp;</td>');
   for ( var j = 0; j < data.nCol; j++ )
   {
      aStr.push('<th width=' + data.nCellWidth + '><span>' +
         data.colHeaders[j] + '</span></th>');
   }
   aStr.push('<td width=20>&nbsp;</td>');
   aStr.push('</tr>');

For each data row, a row header is included at the front. Note: Here, each row header and data value is contained within a <span> element. On the span elements for the value cells, the overflow:hidden style is used to ensure the cells are all the same size. If any cell is allowed to expand, it will cause problems with the scroll bar rendering.

   // data rows
   for ( var i = 0; i < data.nRow; i++ )
   {
      aStr.push('<tr>');

      // row header
      aStr.push('<th valign=top width=' + data.nCellWidth + '><span>' +
         data.rowHeaders[i] + '</span></th>');

      // value cells
      for ( j = 0; j < data.nCol; j++ )
         aStr.push('<td><span style="overflow:hidden;width:' +
            data.nCellWidth + 'px;height:' + data.nCellHeight + 'px; ' +
            'text-align:center;">' + data.values[i][j] + '</span></td>');

The vertical scrollbar is added by including a <td> element at the end of the first row with colspan equal to the number of rows in the grid. There are two parts to the scrollbar; the first part is a scroll indicator, which shows which section of the source data is being viewed and the second part contains the control buttons used to modify the scroll position. In this example I have used the characters ‘^’ and ‘v’ for up and down and ‘<’ and ‘>’ for left and right.

These two parts are overlaid, one on top of the other using the position:absolute style. The button part is above the indicator so the ‘buttons’ may be clicked.


home / programming / javascript / gr / column4 / 1 current pageTo page 2To page 3
[next]

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