Hierarchical drop-downs in Dovetail Agent
Many customers use hierarchical (multi-level) drop-down lists in their Clarify/Dovetail implementations. So, when one level of a select list changes, the down-level lists change as well. Clarify allows up 5 levels for a list.
As an example, in baseline Clarify, there’s a list titled CR_DESC that is a three level list for CPU, Operating System, and Memory. When I change the CPU from PC to Sun, the operating system and memory lists need to change as well.
Default values show:
Changing the CPU to "SUN" changes the O/S and Memory lists:
In a client/server app, this is pretty easy to do, as the onchange event of one list can simply update the down-level lists, either by making a call back to the server, or by accessing data that has been cached on the client machine.
In a web application, this is more difficult. You don’t want to refresh the entire page, as this would be slow, and in general, would suck from a usability standpoint.
DdlHelper
For the past few years, we’ve used a helper utility called DdlHelper. This helper has a server-side component that builds up a multi-dimensional JavaScript array for all of the levels in the list, and then pushes this data down to the client. The client-side component then rebuilds the down-level lists when one changes. Because all of the data is already on the client, this approach is very quick from the client-side. However, the server-side code can be slow and processor intensive when there are a lot of levels and elements in the list. For example, we recently had a customer who had thousands of elements in a 4 level list. The DdlHelper utility could process the list, but it was very slow – slow to the point of unusable.
A more contemporary approach
An alternative approach is to follow an AJAX like pattern, using asynchronous JavaScript to refresh the down-level lists. For example, when the CPU changes, make an async request back to the web server, and just get the specific O/S and Memory elements based on the selected CPU value. Since the Dovetail SDK caches this list data on the web server in memory, this isn’t a database roundtrip – just a roundtrip to the web server. Because we’re only getting the specific list levels that we need (as opposed to all of the list levels), it’s much quicker.
In addition to using this pattern, I also used JSON to pass the data back from the web server to the client. JSON allows me to work with JavaScript objects. On the web server, I can build up an object with the data that I want, "stringify" it, and then return that back to the client. On the client side, I can "parse" the response text back into the same object that I had on the server. Sweet. What’s even better is that there are a plethora of JSON libraries out there to use. I used a JavaScript one.
multiLevelListHelper Object
To make all of this easily reusable, I created a multiLevelListHelper object. The steps to add a new multi-level list to a page are:
1. Include the json.js and multiLevelListHelper.js code files:
<script language="javascript" src="../code/json.js"></script> <script language="javascript" src="../code/multiLevelListHelper.js"></script> |
2. Define the empty HTML select elements (I’ve also added in labels):
<label for="cpu">CPU:</label><select id = "cpu"></select> <label for="os">O/S:</label><select id = "os"></select> <label for="memory">Memory:</label><select id = "memory"></select> |
3. Create a new multiLevelListHelper object in client-side code:
The constructor takes 3 arguments. The name of the list, an array of select element IDs, and an array of initial values. Note that here I am passing in empty strings for the initial values, so the default values (as defined in the database) will be used.
var cpuList = new multiLevelListHelper("CR_DESC", [‘cpu’,’os’,’memory’], [”,”,”]); |
4. Call the populate method on your multiLevelListHelper object:
cpuList.populate(); |
That’s it. The list is rendered, populated, and wired up for onchange events.
Multiple lists on one page
This was actually a little tricky to implement, but yes, you can have multiple hierarchical lists on one page. Create your HTML elements, and then create your multiLevelListHelper objects:
<fieldset> <fieldset> <fieldset>
<script type="text/javascript"> var elementIds = [‘x_system’,’x_casetype’,’x_problem’,’x_symptom’,’x_5th’]; var familyList = new multiLevelListHelper("Family", [‘family’,’line’], [”,”]); var cpuList = new multiLevelListHelper("CR_DESC", [‘cpu’,’os’,’memory’], [”,”,”]); </script> |
Result:
Performance
Overall, this is extremely quick. Running on our LAN, its instantaneous. I am considering adding some type of processing indicator [www.napyfab.com/ajax-indicators/ no longer available], for those operating in slower networks. A task for another day.
Code
For those that want to peek inside the code, here we go.
The constructor for the multiLevelListHelper Object. It’s a simple JavaScript object. It also wires up the onchange events for each select element.
function multiLevelListHelper(_listName, _elementIds, _values){ |
The populate functions, which makes the async call to the web server, as well as the function for when the data is returned back to the client. Notice the JSON.parse method used to turn the response data back into a JavaScript object.
multiLevelListHelper.prototype.populate = function() this.xmlHttp=GetXmlHttpObject(); |
Helper function for adding option elements to a select list and setting its value:
function addOptionsToSelectList(elementId,ArrayOfOptionValues,selectedValue) |
The onchange event handler function. When a select list changes, this function will get called.
Notice that it calls on the same populate method as when the lists are initially built.
function onDropDownChange() var selectElement1 = document.getElementById(listObject.elementIds[0]); |
The PopulateDropDowns.asp page. Notice that we’re creating a JSON object with the data for each level of the list, along with which element should be selected in each level. Then, we "stringifying" the JSON object, and then send this back to the client. As we saw earlier, the client-side code parses this string back into the same JavaScript object.
<%@ Language=JavaScript %> <% function addLevelToJsonObject(whichLevel,recordset,value) var property = "level" + whichLevel + "Value"; try{ Response.Clear(); var jsonObject = new Object(); //Level 1: //Level 2: //Level 3: //Level 4: //Level 5: jsonObject.selectElement1Id = selectElement1Id; %> |