Expandable/ Drill down table

Dear experts,

I would like to create a expandable/ drilldown html table.
For example, a table that displays all the county information of a state within the entire US.
The user should be able to click on an expand button next to the state the view the county information of each state.

The information will be pull from a database using Java.

Any advice are welcome.


Who is Participating?
mmarksburyConnect With a Mentor Commented:
There are a few ways...

1. Pull all the information on page load.

2. Pull only the state information on page load and pull the counties on request using AJAX.

I'd go for #2 and get some AJAX experience under your belt (if you dont already have it).

So for example....with each state you might have...

     <a href="javascript:GetCounties('CA');">California</a><br />
     <div id="CA_Counties"></div>

Then you would write a Javascript function that uses AJAX to call the server and get all Counties for the state "CA".  Parse the response XML and populate the CS_Counties DIV with the values.
ColcobConnect With a Mentor Commented:
I dont have any AJAX experience, so I can only tell you how to do it via mmarksbury's method 1.

I've done a technical library browser with pop-out subsections like this.

First, you have a javascript function called toggle, which simply changes the display style from block to none, or none to block.  Thus you can call that function on any block level element and it will show or hide it, regardless of its current state (i havent got the code to hand, but if its something you couldnt do yourself, I can dig it out).

You'll need to pull the full dataset of states and counties into the page, presumably as a two-level array, then its a case of creating your tables with hidden sections for the counties which are revealed when you click on the states.

Unfortunately, I dont think you can show/hide individual rows in a table, as they aren't block level elements, so I had to create the thing out of lots of separate tables and bunch them together with the styling. EG:

<table class="state"><tr><td><a onClick="javascript: toggle(stateCounties1)">State1</td></tr></table>
     <table class="county" id="stateCounties1">

<table class="state"><tr><td><a onClick="javascript: toggle(stateCounties2)">State2</td></tr></table>
     <table class="county" id="stateCounties2">

So you use CSS to set table.county to display: none; so the counties dont show up to start with, all the id's and arguments in the toggle function need to be set by the values from the db so they tie up with eachother, and you should be good to go.

Note, I did this a while ago, and I suspect there is a more modern sematic way of doing it with Lists instead of Tables, but I havent tried it myself.  A UL is a block level element, so I'm sure you can probably use UL's instead of tables.

scrathcyboyConnect With a Mentor Commented:
Just about everyone is doing this in javascript, like the two responses above.  Even microsoft does this on their "knowledgebase" webpages.  The issue you have to deal with is -- if you do it all in javascript, within the page, that page will become HUGE, and take a long time to load.

Therefore, I option 2 of mmarksburys above is definitiely the best.  Use a DIV for each county data, and keep the county data in separate files, and read those files into the DIV if and only if a person clicks on the link to the counties of that state.  A little more code, but a much faster site response.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.