
note - Thu, 27 Jul 2006 15:36:57 GMT
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



