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

Return to article


Listing 3. Java class for tag library

package com.ibm.consolidatedTable;

import java.util.StringTokenizer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class ConsolidatedTableTag extends TagSupport {
   private String[] columns;
   private String[][] content;

   public int doStartTag() throws JspException {

      JspWriter out = pageContext.getOut();
      try { 
         out.write("<script>"
            + "\n\tvar columnNames = new Array();"
            + "\n\tvar columnSelected = new Array();"
            + "\n\tvar columnChecked = new Array();"
            + "\n\tvar selectedIndex = -1;"
            + "\n\tvar results = new Array();");
         for (int i = 0; i < columns.length; i++) {
             out.write("columnNames[" + i + "] = '" + columns[i] + "';");
             out.write("columnSelected[" + i + "] = " + (i == 0) + ";");
         }

         for (int i = 0; i < content.length; i++) {
            out.write("results[" + i + "] = new Array();");
            for (int j = 0; j < content[i].length; j++) {
                out.write("results[" + i + "][" + j + "] = '" + content[i][j] + "';");
            }
         } 

         out.write("\n\tfunction drawTable() {"
            + "\n\t\tvar destination = document.getElementById(\"contents\");"
            + "\n\t\tvar divContents = \"\";"
            + "\n\t\tdistance=0;"
            + "\n\t\tfor (i = 0; i < columnNames.length; i++) {"
            + "\n\t\t\tif (columnSelected[i]) {"
            + "\n\t\t\t\tdivContents += '<div style=\"position:absolute;left:'"
            + distance + 'px;top:0px;height:20px;width:300px;overflow-y:none;'"
            + "\n\t\t\t\t\t\t+ '\"><table border=1 style=\"width:100%\">'"
            + "\n\t\t\t\t\t\t+ '<tr>'"
            + "\n\t\t\t\t\t\t+ '<td onmouseover=\"changeRow(-1);\"" 
            + " style=\"font-weight:bold;font-size:larger;background-color:#BBCEDB;\">'"
            + "\n\t\t\t\t\t\t+ columnNames[i]"
            + "\n\t\t\t\t\t\t+ '</td></tr></table></div>'"
            + "\n\t\t\t\t\t\t+ '<div style=\"position:absolute;left:' + distance 
            + 'px;top:20px;height:323px;width:300px;overflow-y:scroll;'"
            + "\n\t\t\t\t\t\t+ '\"><table border=1 style=\"width:100%\">';"
            + "\n\t\t\t\tfor (j = 0; j < results[i].length; j++) {"
            + "\n\t\t\t\t\tdivContents += '<tr>'"
            + "\n\t\t\t\t\t\t\t+ '<td style=\"font-size=larger;'" 
            + "\n\t\t\t\t\t\t\t+ (selectedIndex == j ? 'background-color:cyan;font-weight:bold' : '')"
            + "\n\t\t\t\t\t\t\t+ '\" onmouseover=\"changeRow(' + j + ')\">'" 
            + "\n\t\t\t\t\t\t\t+ results[i][j] + '</td></tr>';"
            + "\n\t\t\t\t}"
            + "\n\t\t\t\tdivContents += '</table></div>';"
            + "\n\t\t\t\tdistance += 300;"
            + "\n\t\t\t} else {"
            + "\n\t\t\t\tdivContents += '<div style=\"position:absolute;left:' + distance 
            + 'px;top:0px;height:350px;width:30px;background-color:'"
            + "\n\t\t\t\t\t\t+ (i % 2 == 0 ? '#BBCEDB;' : '#eeeeee;') 
            + 'writing-mode:tb-rl;text-align:center;'"
            + "\n\t\t\t\t\t\t+ 'text-decoration:underline;font-weight:bold;font-size:larger;\"'"
            + "\n\t\t\t\t\t\t+ ' onmouseover=\"changeColumn(' + i + ')\">'" 
            + "\n\t\t\t\t\t\t+ (selectedIndex == -1 || selectedIndex >= results[i].length" 
            + "\n\t\t\t\t\t\t? (columnNames[i].length > 30 ? columnNames[i].substring(0, 30) 
            + '...' : columnNames[i])" 
            + "\n\t\t\t\t\t\t: (results[i][selectedIndex].length > 30 
            ? results[i][selectedIndex].substring(0, 30) + '...' : results[i][selectedIndex]))"
            + "\n\t\t\t\t\t\t+ '</div>'; "
            + "\n\t\t\t\t\t\tdistance += 30;"
            + "\n\t\t\t}"
            + "\n\t\t}"

            + "\n\t\tdestination.innerHTML = divContents;"
            + "\n\t}"
 
            + "\n\n\tfunction changeColumn(index) {"
            + "\n\t\tfor (i = 0; i < columnSelected.length; i++) {"
            + "\n\t\t\tcolumnSelected[i] = false;"
            + "\n\t\t}"
            + "\n\t\tcolumnSelected[index] = true;"
         + "\n\t\tdrawTable();"
            + "\n\t}"

            + "\n\tfunction changeRow(index) {"
            + "\n\t\tselectedIndex = index;"
            + "\n\t\tdrawTable();"
            + "\n\t}"
            + "\n</script>"
            + "\n<div id=\"contents\"> </div>"
            + "\n<script>"
            + "\n\tdrawTable();"
            + "\n</script>");
      } catch (Exception e) {
          e.printStackTrace();
      }
      return EVAL_BODY_INCLUDE;
   }

   public void setColumns(String columns) {
      StringTokenizer tokens = new StringTokenizer(columns, ",");
      String[] parsed = new String[tokens.countTokens()];
      for (int i = 0; i < parsed.length; i++) {
          parsed[i] = tokens.nextToken().trim();
      }
      this.columns = parsed;
   }

   public void setContent(String content) {
      StringTokenizer bigTokens = new StringTokenizer(content, ";");
      String[][] parsed = new String[bigTokens.countTokens()][]; 
      for (int i = 0; i < parsed.length; i++) {
         StringTokenizer littleTokens = new StringTokenizer(bigTokens.nextToken(), ",");
         parsed[i] = new String[littleTokens.countTokens()];
         for (int j = 0; j < parsed[i].length; j++) {
             parsed[i][j] = littleTokens.nextToken().trim();
		 		 		 }
      }
      this.content = parsed;
   }
}

Return to article