Universally Related Popup Menus AJAX Edition: Part 3 / Page 3 | WebReference

Universally Related Popup Menus AJAX Edition: Part 3 / Page 3


[prev]

Universally Related Popup Menus AJAX Edition: Part 3[con't]

We have to initialize responseString to an empty string because we will be appending to it to create the response (line 43). Looping through a recordset is quite easy to do. We just test for the End Of File (EOF) cursor marker (line 44). As long as we haven't encountered it, we concatenate the JavaScript code that will be executed on the client-side (line 45). We don't have to know the list index because we are always adding the new Option at the end of the options array. That position is denoted by the options.length property (line 46). We have to escape double quotes in the description field because it will cause a syntax error in the JavaScript engine (line 47). The ID field doesn't need any doctoring because it only contains digits (line 48). movenext sets the cursor on the next record (line 50).

Before sending back the response string, we check for an error since we told the script to go ahead no matter what (line 53). If the Err object has been populated, the error message is passed along to the AJAX XmlHttpRequest object to display (line 54). Alternatively, if there are no list items for that parent ID, a value of "N/A" is returned to display in the list (line 56).

Finally, the responseString is returned to the client's browser (line 60), and the objects are cleaned up (line 62).

Future Enhancements

Nothing in life is ever perfect, and despite having improved the URPMs several times over, there are still some things that can be improved upon.

For instance, the code that clears the list items is overzealous in its obliteration of all the list items. It would be more efficient to remove items only if there are leftovers remaining after the new ones have been created. The options' text and values could be changed for existing ones instead of creating new options.

Why not have default indexes for each list or even each linked URPM's list? That would allow each item of a list to result in a different default listIndex in the child list. It sounds like a good idea, but it's a lot of work. I'll let someone else run with it!

The ASP code to call the proc could be better developed for multiple word names. As it stands, and ID of "kitchen sinks" would call a proc called "getKitchen sinks". Not bad, but it looks weird and the space could be a problem.

For homework, implement a caching system on the client-side to store previously-selected list items and their related lists. That would potentially eliminate some server calls if the user decided to go back to a previous selection.

Conclusion

I hope you enjoyed this installment of the URPMs. I think that you'll agree that AJAX was tailor made for this sort of application. Although I wrote the script pretty much from scratch, with the advent of frameworks like Struts and Spring, this type of thing is bound to get a lot easier in the years to come. Until then, AJAX remains a giant leap forward in the quest for a rich web user experience.

References

About the Author

Robert Gravelle is a Senior Programmer/Analyst for the Canadian Border Services Agency as well as a freelance IT consultant. He specializes in Java, Perl, ASP, Microsoft application automation, JavaScript, VBScript, Web Development, and multi-tier systems. Feel free to contact him, but due to the volume of emails received, he cannot guarantee a response to every message.

Original: February 6, 2008

Digg This Add to del.icio.us


[prev]