spacer

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

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

Ad Copywriter
Aquent
US-VA-Richmond

Justtechjobs.com Post A Job | Post A Resume
Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

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]



JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
Intel Article: Using Power & Display Context in the Intel Mobile Platform SDK
Internet.com eBook: Real Life Rails
IBM SCA Center Article: Simplifying Composite Applications with Service Component Architecture
Intel PDF: Quad-Core Impacts More Than the Data Center
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Intel PDF: Analysis of Early Testing of Intel vPro in Large IT Departments
Internet.com eBook: Best Practices for Developing a Web Site
Intel PDF: IT Agility through Automated, Policy-based Virtual Infrastructure
IBM CIO Whitepaper: The New Information Agenda. Do You Have One?
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
IBM Whitepaper: How are other CIOs driving growth?
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Actuate Download: Free Visual Report Development Tool
Red Gate Download: SQL Backup Pro
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
IBM SCA Download: Start Building SCA Applications Today
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags

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

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