developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Laptop Batteries
Promotional Gifts
Web Design
Online Shopping
Promotional Products
PDA Phones & Cases
Shop
GPS Devices
Condos For Sale
Rackmount LCD Monitor
Domain registration
Phone Cards
Promos and Premiums
KVM Switches

 


Web Devs:
Moonlight as a Game Developer and Win Cool Prizes by Accepting the RIA Run Challenge

Now, your mission--should you choose to accept: Take your shot at gaming stardom if you think you might have what it takes to build a cool RIA game and you could win an Xbox 360 or other fabulous prizes. Hurry! You only have until May 15, 2008 to enter. »

 
Article:
Leveraging Your Flash Development with Silverlight

You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here. »

 
Article:
What Does it Take to Build the Best RIA?

With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application. »

 
Related Article -
The Web 2.0 Movement Is Here. But What Does It Mean to You?
The Twelve Days of AJAX
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Implementing Dynamic Scroll with Ajax, JavaScript, and XML
By Vlad Kofman

Go to page: 1  2  Next  

In this article, I will discuss implementing an Ajax-enabled scrollable table, in which rows are dynamically fetched from the server, as the user scrolls, without the whole page refreshing. This technique is unique and very convenient for implementing pages with a lot of dynamic content in a readable form; for instance, a live news feed. As new items are added to the table, the vertical scroll bar will resize and the never-ending scroll will continue. The asynchronous fetching of the data is done with AJAX (Asynchronous JavaScript and XML). The parsing and dynamic row insertions are done in JavaScript, the requests are done with the XMLHttpRequest object, and the table appearance is controlled by the CSS.

The implementation of the dynamic scroll is possible mostly because of the introduction of the XMLHttpRequest object in the modern browsers. This JavaScript object can send requests asynchronously and receive responses from the server, without page refresh. Many web sites currently participating in the Web 2.0 design methodology often use a lot of Ajax to offer rich client interfaces, the XMLHttpRequest is extensively used on such sites and content of the page changes dynamically based on the user action.

The scrollable table is a good example of a rich client interface, which uses the Web 2.0 approach to the web content usability. The dynamic scroll is also possible in part because of the new CSS2 standards, which are supported by the main browsers as well. In the sample code, I will show how to implement scrollable table that occupies some part of the screen, but if CSS were not supported and only Ajax were, the dynamic table would just resize (and scroll) the whole page, and not part of it.

Here is the example of a dynamic table on the Google reader site. Note that there are 100 items in the table on the first screenshot and, as the vertical scroll bar is moved down more rows appear, the number changes to 140, and the scroll bar also resizes. All other elements remain in the same positions on the page.

Figure 1: A dynamic table showing 100 items.

Figure 2: The same table after 40 items have been added. Note the position of the scroll.

Presentation of the Dynamic Table

To create the scroll behavior of any HTML element (in this case a table), it needs to be enclosed in a scrollable area (or viewport) defined by an outer element (in this case a DIV), which should be smaller then the total table height. If there is no outer element (no viewport), the inner element would just grow on the page in place, and after a while the whole page would resize and start to scroll (within the browser's page viewport). For example, a div element with height of 100 pixels containing table with height of 200px would achieve this table/viewport encapsulation behavior.

<div style="height:100px; width:50px;
            overflow:auto; overflow-x:hidden;">
   <table style="height:200px;">
      <tr><td>vlad</td></tr>
   </table>
</div>

Figure 3: Example code to generate scroll behavior

Note: If you set the same properties on these two elements separately, only the div would scroll, but not the table.

Here is an example code and what looks like in the browser.

<table style="height:50px; width:100px;
              overflow:auto;overflow-x:hidden;">
   <tr><td><p>vlad 1</p></td></tr>
   <tr><td><p>vlad 2</p></td></tr>
   <tr><td><p>vlad 3</p></td></tr>
   <tr><td><p>vlad 4</p></td></tr>
   <tr><td><p>vlad 5</p></td></tr>
</table>

<hr/>

<div style="height:100px; width:100px;
            overflow:auto;overflow-x:hidden;">
   <p>Vlad 1 </p>
   <p>Vlad 2</p>
   <p>Vlad 3</p>
   <p>Vlad 4</p>
   <p>Vlad 5</p>
</div>

Here is a screen shot of the HTML above.

Figure 4: Visual representation of HTML element properties (source MSDN)

Notice that the table height is set to 50, but it still does not scroll.

The two CSS properties overflow:auto; and overflow-x:hidden;* actually make the scroll bar appear on the right within the div element and make the table scroll within the div. The browser's CSS engine must support these two properties. Otherwise, the table would only grow in place without the scroll bar.

* These are CSS2 properties. If content in an element falls outside the element's rendering box for a number of reasons (negative margins, absolute positioning, content exceeding the width/height set for an element, etc.), the 'overflow' and 'overflow-x' properties describe what to do with the content that exceeds the element's width.

These CSS properties only define the appearance of the div and table, but JavaScript APIs interface with the scroll properties. To understand the implementation of a dynamic table, you need to know properties that measure HTML elements' dimension and location on the page. These properties can be programmatically accessed and changed in JavaScript and, thankfully, APIs are the same across most major browsers.

HTML element properties

Here is the cheat sheet of available HTML element properties for HTML BODY and a single DIV element in the center of the page. As you can see, there are a lot of them, but the three of the most interest are scrollTop, scrollHeight, and clientHeight.

Figure 5: Visual representation of Div element properties (source MSDN)

The scrollTop specifies the position of the content upper bound relative to the visible portion of the viewport (defined by the div's height and width), as the user scrolls. scrollTop would indicate the distance in pixels the content moved. The scrollHeight specifies the actual height in pixels of the inner element and clientHeight specifies height of the visible content (viewport height). In my case, the viewport is the outer div element and the content is the inner TABLE. The Height and Top properties do not account for element's borders, margins, or padding, and if your elements have any of these attributes, look into the offset group of properties, among others. Similarity to the height, there are width properties to deal with horizontal scrolling calculations.

Every element placed on the page has these properties and the way elements look (and behave) depends on the values of these properties.

Here is simplified depiction of the div element's properties in the page.

Figure 6: Code to generate scroll behavior within 500px Div

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Architecture & Design Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Intel Go Parallel Portal: Translating Multicore Power into Application Performance



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES