« [Tip] JProgressBar bean | Main | [Challenge] Week 3/7/05: Code Contortions (answer) »

March 21, 2005

[Tip] HTML subsummary example

Here is an example file from Bob Cusick that shows you how to do a two-level subsummary report in html.

Main method code listing:

//build the first SQL query to get all customers
//sort by country, city, companyname, contactname

//Get a dataset based on query
var maxReturnedRows = 10000;

//the SQL query
var query = "select country, city, companyname, contactname, phone, fax " +
    " from customers order by country, city, companyname, contactname"
    
var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, maxReturnedRows);

//set a variable to the max number of customer records
var max = dataset.getMaxRowIndex()

if(max > 0)
{
    //it returned some data
    
    //setup the HTML
    var html = "<html>\n<head>\n<style type='text/css'>\n .text {font-family: Tahoma; font-size: 11pt;}\n</style>" +
        "</head>\n<body class='text'>"
        
    //setup the outer table
    html += "\n<table border=0 cellpadding=2 cellspacing=0>"
    
    //setup some more variables
    var curCountry = ''
    var curCity = ''
    var newCountry = false
    var newCity = false
    
    //begin first loop through all the records
    
    for( var i = 1 ; i <= max ; i++ )
    {
        //see if we're looking at the same country or a different one
        if(curCountry != dataset.getValue(i, 1))
        {
        
            newCountry = true
            
            if(i > 1)
            {
                //close customer detail
                html += "\n    </table> < --CLOSE CUSTOMER- >"
                
                //close city table
                html += "\n  </table> < --CLOSE CITY- >"
                
                //close the row showing the country
                html += "\n</tr> < --CLOSE COUNTRY- >"
            }
            
            //get the current country
            curCountry = dataset.getValue(i, 1)
            
            //make a new row to show the country
            html += "\n<tr><td class='text' valign='top' colspan=2><b>" + curCountry + "</b></td>"
            
            //COUNTRY APPEAR ABOVE CITIES
            if(globals.showAbove == 1)
            {
                html += "</tr><tr><td class='text' valign='top'>"
            }
        }
        else
        {
            newCountry = false
        }
        
        //see if we are looking at the same city or not
        if(curCity != dataset.getValue(i,2))
        {
            
            newCity = true
            curCity = dataset.getValue(i, 2)
            
            if(i > 1 && !newCountry)
            {
                //close customer detail
                html += "\n    </table> < --CLOSE CUSTOMER2- >"
                
                //close city table
                html += "\n  </table> < --CLOSE CITY2- >"
            }
            
            //make a new table inside the country row
            //to display the city and customer details
            
            //city
            html += "\n\n     <table border=0 cellpadding=2 cellspacing=0 width='100%'>" +
                "\n        <tr><td class='text' valign='top'><b>" + curCity + "</b></td></tr>"
            
            //customer detail
            html += "\n\n     <table border=0 cellpadding=2 cellspacing=0 width='100%'>"
            

            //draw headings
            html += "\n        <tr><td class='text' valign='top'><i>Company</i></td>" +
            "\n        <td class='text' valign='top'><i>Contact</i></td>" +
            "\n        <td class='text' valign='top'><i>Phone</i></td>" +
            "\n        <td class='text' valign='top'><i>Fax</i></td></tr>"

            
            //draw first row of data
            html +=    "\n        <tr><td class='text' valign='top'>" + dataset.getValue(i, 3) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 4) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 5) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 6) + "</td></tr>"
            
            
        }
        else
        {
            newCity = false
            
            //same city, same country
            html += "\n\n     <tr><td class='text' valign='top'>" + dataset.getValue(i, 3) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 4) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 5) + "</td>" +
            "\n        <td class='text' valign='top'>" + dataset.getValue(i, 6) + "</td></tr>"
        }
        
    }
    //close the overall table
    html += "\n</tr>\n</table>\n</body>\n</html>"
    
    globals.HTML = html
}

| Posted by David Workman on March 21, 2005 at 06:28 PM in Tips | Permalink

Comments

Post a comment