Implementation of the Dynamic Table
To implement the table, I have separated the presentation into a CSS style called "scrolltable" and created a div with id "new_items_div" and an inner table with id "new_items".
The "status" div's purpose is to display various messages.
Click here for a larger image.
Figure 7: Code to detect scroll on the page
The algorithm, which detects scroll position, checks to see whether the distance already scrolled is greater than or equal to the content height.viewport height. I also add 20 pixels to the distance to account for the scroll bar size and to make sure the position of the scroll bar is a little prior to very end, before the new row fetch occurs.
Fetch if [scroll Top distance >= (scroll Height - client Height) + 20]
If the user scrolls up after scrolling down, no new rows are fetched because this condition will not be true.
Figure 8: Screen shot of the final table
If the condition is met when detectScoll executes, a new JavaSript function fectchAction is called. I also update the "status" div with the total row count.
The fetchAction function below executes the asynchronous call to the server, using the XMLHttpRequest object, and assigns a callback function, readFeed, to process the response. The server side implementation is irrelevant; as long as it returns valid XML, it can be done in any language. In this case, it's PHP, but it can be Java, .NET, and so on.
Dynamic Parsing and Insertion of the Data
For the front end to properly parse the response and dynamically append a row to the table, the back end should return XML similar to this code snippet:
<?xml version='1.0' encoding='ISO-8859-1'?> <channel> <item> <title>Vlad</title> <body><![CDATA[some text]]></body> </item> </channel>
The callback function readFeed is called when the server responds with the XML. The first thing this function does is obtain a handle on the XML structure, xmlHttp.responseXML.documentElement. It then gets all elements' names "item" and loops through them, pulling out title and body. The title and body are then passed to the helper function addRow that does the dirty work of appending the data to the table. addRow gets the table object, inserts a new row at the end, and inserts the title and body HTML.
Here is the implementation of the readFeed function:
And here is the implementation of the addRow function. For the complete source, please see the download section.
In the sample page, initially when it loads there are 10 rows; when the user moves the scroll bar down, 10 more rows appear. The end result looks something like this:
This is all the logic needed to make an Ajax-enabled dynamic table.
A possible downside to this approach is a potential of a client requesting a lot of data and running out of memory. Because this scroll is only limited by the amount of data on the server, the client may request all of it; this technique will store it in the client's memory.
You can download the code that accompanies this article here.
About the Author
Vlad Kofman works on enterprise-scale projects for the major Wall Street firms. He has also worked on defense contracts for the U.S. government. His main interests are object-oriented programming methodologies, UI, and design patterns.
Page 2 of 2