Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Create easy-to-view tables

Consolidate your table data by using a mouseover-controlled tabular display

Jeff DePree (jtdepree@us.ibm.com), Software Engineer, Pervasive Computing, IBM
Jeff is a computer engineering student at the University of Florida and an intern in the pervasive computing division of IBM. He has worked extensively with J2EE technologies as well as graphical user interfaces. You can reach Jeff at jdepree@ufl.edu

Summary:  Tired of scrolling for table data? With this consolidated table widget, you simply point to all the information you need -- even on devices with small screens. This article describes how you can reduce the dimensions of a tabular display while retaining the ability to view all fields of a selected column and row simultaneously.

Date:  21 Sep 2004
Level:  Introductory

Activity:  3707 views
Comments:  

Developers often use a tabular format to present information; however, to use the traditional grid layout, you must have enough horizontal space to display all columns simultaneously. The only alternative is to provide a scrollable interface in which you can view only a portion of a column or columns at any given time. This isn't exactly user friendly. Not only does it remove elements from context, but you must scroll sideways while remembering what horizontal you are traversing as well as the other elements in that horizontal.

This article explains a way to display many more columns at one time. With all data available in one screen, the user can make connections immediately without the need to frantically scroll back and forth as he struggles to remember the column values in previous screens.

Start with structure

The graphical user interface you'll use consists of a fully visible column, which shows the column header as well as a scrollable list of row elements in that column. To the left and right of this column are the other column headers in the table; you'll display these vertically so the table consumes less horizontal space than in a traditional two-dimensional grid. Note the comparison of this approach and the traditional grid in Figure 1.


Figure 1. Consolidating a table into a smaller horizontal space
Comparison of old and new approach to table

By selecting the row elements in the visible column, the values for that row in the other columns appear; likewise, by selecting the adjacent vertical tabs, the currently visible column becomes a tab while the selected tab becomes the visible column. In this format, the only dimension restriction is the width of the tabs and that of the one visible column.


Implementing in JavaScript

You can implement this structure in any number of graphical formats. I provide a JavaScript interface as an example. First, draw the current state of the table (depending on which column is visible) by dynamically replacing the current contents of a div tag with the appropriate form of the interface; this will consist of div tags for all the columns, with the visible column containing an actual HTML table. Set style attributes to make the tabs' text vertical and create an alternating color scheme. The tabs respond to mouseover events, redrawing the table based on the cursor's location, as illustrated by Figures 2 through 5.


Figure 2. Column "c" mouseover
Mouseover of one column

Figure 3. Column "b" mouseover
Mouseover of another column

Figure 4. Mouseover of row with elements 4, 9, 14, 19, 24
Mouseover of a given row

Figure 5. Mouseover of row with elements 5, 10, 15, 20, 25
Mouseover of a second row

JavaScript code for table

The code in Listing 1, for the example table above, redraws the page completely when it first loads and whenever a mouseover event occurs.


Listing 1. Source code for sample table
<HTML>
<BODY>
<script>
  var columnNames = new Array();
  var columnSelected = new Array();
  var columnChecked = new Array();
  var selectedIndex = -1;
  var results = new Array();columnNames[0] = 'a';columnSelected[0] 
     = true;columnNames[1] = 'b';columnSelected[1] = false;columnNames[2] 
     = 'c';columnSelected[2] = false;columnNames[3] = 'd';columnSelected[3] 
     = false;columnNames[4] = 'e';columnSelected[4] = false;results[0] 
     = new Array();results[0][0] = '1';results[0][1] = '2';results[0][2] 
      = '3';results[0][3] = '4';results[0][4] = '5';results[1] 
      = new Array();results[1][0] = '6';results[1][1] = '7';results[1][2] 
      = '8';results[1][3] = '9';results[1][4] = '10';results[2] 
      = new Array();results[2][0] = '11';results[2][1] = '12';results[2][2] 
      = '13';results[2][3] = '14';results[2][4] = '15';results[2][5] 
      = '16';results[2][6] = '17';results[2][7] = '18';results[2][8] 
      = '19';results[2][9] = '20';results[2][10] = '21';results[2][11] 
      = '22';results[2][12] = '23';results[2][13] = '24';results[2][14] 
      = '25';results[2][15] = '26';results[2][16] = '27';results[2][17] 
      = '28';results[2][18] = '29';results[2][19] = '30';results[2][20] 
      = '31';results[2][21] = '32';results[2][22] = '33';results[2][23] 
      = '34';results[3] = new Array();results[3][0] = '16';results[3][1] 
      = '17';results[3][2] = '18';results[3][3] = '19';results[3][4] 
      = '20';results[4] = new Array();results[4][0] = '21';results[4][1] 
      = '22';results[4][2] = '23';results[4][3] = '24';results[4][4] = '25';
  function drawTable() {
   var destination = document.getElementById("contents");
   var divContents = "";
   distance=0;
   for (i = 0; i < columnNames.length; i++) {
      if (columnSelected[i]) {
           divContents += '<div style="position:absolute;left:' + distance 
              + 'px;top:0px;height:20px;width:300px;overflow-y:none;'
              + '"><table border=1 style="width:100%">'
              + '<tr>'
              + '<td onmouseover="changeRow(-1);" '
              + 'style="font-weight:bold;font-size:larger;'
              + 'background-color:#BBCEDB;">' + columnNames[i]
              + '</td></tr></table></div>'
              + '<div style="position:absolute;left:' + distance 
              + 'px;top:20px;height:323px;width:300px;overflow-y:scroll;'
              + '"><table border=1 style="width:100%">';
           for (j = 0; j < results[i].length; j++) {
              divContents += '<tr>'
                  + '<td style="font-size=larger;'
                  + (selectedIndex == j ? 'background-color:cyan;'
                  + 'font-weight:bold' : '') 
                  + '" onmouseover="changeRow(' + j + ')">'
                  + results[i][j] + '</td></tr>';
           }
           divContents += '</table></div>';
           distance += 300;
      } else {
           divContents += '<div style="position:absolute;left:' + distance 
              + 'px;top:0px;height:350px;width:30px;background-color:'
              + (i % 2 == 0 ? '#BBCEDB;' : '#eeeeee;') 
              + 'writing-mode:tb-rl;text-align:center;'
              + 'text-decoration:underline;font-weight:bold;'
              + 'font-size:larger;"'
              + ' onmouseover="changeColumn(' + i + ')">'
              + (selectedIndex == -1 || selectedIndex >= results[i].length
              ? (columnNames[i].length > 30 
              ? columnNames[i].substring(0, 30) + '...' : columnNames[i])
              : (results[i][selectedIndex].length > 30 
              ? results[i][selectedIndex].substring(0, 30) 
              + '...' : results[i][selectedIndex]))
              + '</div>'; 
              distance += 30;
      }
    }
    destination.innerHTML = divContents;
  }

  function changeColumn(index) {
    for (i = 0; i < columnSelected.length; i++) {
      columnSelected[i] = false;
    }
    columnSelected[index] = true;
    drawTable();
  }
  function changeRow(index) {
    selectedIndex = index;
    drawTable();
  }
</script>
<div id="contents"> </div>
<script>
  drawTable();
</script>
</BODY>
</HTML>

Using a tag library

Since you want to rapidly change the table contents without worrying about the implementation details, you can provide a tag that generates a consolidated table given only the data values. To this end, you can use a tag library that specifies a custom CTable tag, as shown in Listing 2.


Listing 2. Tag library for consolidated tables
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
  "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>CTable</shortname>

  <tag>
     <name>Table</name>
        <tagclass>com.ibm.consolidatedTable.ConsolidatedTableTag</tagclass>
     <bodycontent>JSP</bodycontent>
     <info>
        Consolidated table.
     </info>
     <attribute>
        <name>columns</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>

     <attribute>
        <name>content</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
     </attribute>
  </tag>

</taglib>

This library must call a Java class, shown in Listing 3, to generate the appropriate JavaScript code for the specified values.

With the tag library in place, you can generate the sample table using only the code in Listing 4.


Listing 4. HTML document using the tag library
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/classes/tld/consolidatedTable.tld" prefix="CTable" %>
<HTML>
<BODY>
<CTable:Table columns="a,b,c,d,e" content="1,2,3,4,5;6,7,8,9,10;11,12,13,14,15,
    16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34;16,17,18,19,20;21,
    22,23,24,25;"/>
</BODY>
</HTML>


In conclusion

With the implementation I've just described, you can display a consolidated table in any JavaScript-enabled browser. By applying these concepts to other programming languages, you can make similar interfaces available for other GUI frameworks, such as Swing and Standard Widget Toolkit (SWT). And by employing this technology on devices with minimal screen dimensions or to a table with a large number of fields, you greatly enhance an application's ease-of-use.


Resources

About the author

Jeff is a computer engineering student at the University of Florida and an intern in the pervasive computing division of IBM. He has worked extensively with J2EE technologies as well as graphical user interfaces. You can reach Jeff at jdepree@ufl.edu

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=15103
ArticleTitle=Create easy-to-view tables
publish-date=09212004
author1-email=jtdepree@us.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers