search the site  

Enter search terms:


subscribe to newsletters   WebReference.com logo   WebReference.com
dev the Web
tip archive  •   about  •   contact  •   jobs  •   sitemap


[next]

How to Create an Ajax Autocomplete Text Field: Part 8

By Rob Gravelle

Digg This Add to del.icio.us

Lotus Notes Developer
AMS Staffing Solutions
US-OH-Columbus

Justtechjobs.com Post A Job | Post A Resume

You may also enjoy:

Adding a Scrollbar

In the last installment, we added flexibility to the layout of the Autocomplete form elements by converting the HTML table-based design to a CSS one. We also introduced a means of passing parameters to an included JSP page in order to turn our Autocomplete text field into a versatile and self-contained Web control. This week we look at how to add a vertical scrollbar to the list. We're also going to use a parameter to set the list size, so a vertical scrollbar will appear when the number of items exceeds it, just like an HTML <SELECT> field (See Figure 1)

The Autocomplete List Container

Adding scrollbars to a table is problematic because they always expand to accommodate their contents. While it's possible to set a minimum height by setting the height attribute, nothing stops it from exceeding that value. The solution is to add a containing <DIV> around the table, since they support scrollbars and won't necessarily grow with their contents. We'll add some functionality to create and keep track of the containing <DIV> to the init() function. Here, we add the container variable to the global variable declarations:

We can create the <DIV> element in the same way that we created the <TABLE>, by using the DOM createElement( String tagName ) method. In the with closure, we set the ID to "container" so our ID rule will be applied to the <DIV> element. Now, we add the completeTable to the container <DIV> and add that to the autocompleteControl. The following code replaces the autocompleteControl.appendChild(completeTable); line:

The visibility must switched on again in the parseMessages() function in order to see the list:

Similarly, the visibility must be turned off in the clearTable() function:

The Container CSS Rule

In the AutocompleteControl.css file, we add a new rule for our container:

The position and border were transferred from the completeTable ID rule. The background was moved from the <TR> tag rule. The visibility property is set to "hidden" so the list won't appear until a character has been entered in the textbox, the results returned and added to the list.

If you were to try out the control in the browser, you would notice a border of one pixel around the table. That's a side effect from creating the table in JavaScript last week (See Figure 2)

There are two simple methods to remove the border. The first would be to set it in JavaScript using the border property, or we can set it in the CSS file by adding the following rule:

The border-collapse property determines whether the table borders are collapsed into a single border or detached as in standard HTML. The space between the table cells is the result of the default detached border style, so collapsing it removes the space.

The Scrollbar CSS Class

The key to displaying a scrollbar is the overflow property, along with max-height. It tells the browser what to do when an element's height exceeds the max-height's value. There are four possible overflow values:

Value Description
visible Default. The content isn't clipped. It renders outside the element.
hidden The content is clipped, but the browser doesn't display a scrollbar to see the rest of the content. 
scroll The content is clipped, but the browser displays a scrollbar to see the rest of the content.
auto If the content is clipped, the browser should display a scrollbar to see the rest of the content.

The "scroll" and "auto" values are the only ones that cause a scrollbar to appear. The difference between the two is that "scroll" will always display the horizontal and vertical scrollbar tracks, minus the scrollbar, even when not required. The "auto" value will cause the scrollbar to only appear once the element exceeds its max-height. Since we don't want a horizontal scrollbar or track to appear, "auto" is the value we use.

Now for the bad news. The max-height property is only supported by DOM-compliant browsers; hence, Internet Explorer 7+ recognizes it, but not version 6. Previous versions of Internet Explorer do support the height property, which achieves almost the same effect, as we'll see later. The overflow-x and y properties are included for the Internet Explorer and Safari browsers because they will display both vertical and horizontal scrollbars without them. Here's what the scrollbar class rule should look like:

Let's have a look at our list in the browser to see how the scrollbar performs. Select the AutocompleteSearchCSS.jsp file in the "Project Explorer" pane and select "Run" => "Run As" => "1 Run on Server" from the main menu to launch the Web Application in a browser.

Type an 's' in the search textbox to retrieve a long list. Since the table height will exceed the value of "100 pixels" that we set in the CSS file, we should see a vertical scrollbar. The following screenshot, which was taken in Internet Explorer 6, only displays a vertical scrollbar, because it's using the overflow-x and y properties (See Figure 3).

We're getting there, but there are still a few issues to contend with:

  1. Most browsers don't increase the width of the containing <DIV> to adjust for the scrollbar, so it's clipping the right sixteen-or-so pixels of the table.
  2. Another, more apparent problem is that, in Internet Explorer 5/6, the substitution of height for the max-height property hasn't served us well. If the list height is less that 100 pixels, it remains at that size (See Figure 4).
  3. Finally, since we're setting the list size in pixels, and not by number of items, the vertical scrollbar could appear when it's not really necessary, as in this case (See Figure 5)


[next]

Recent Articles

WebReference.com site name
Anatomy of an Ajax Application
Popular JavaScript Framework Libraries: An Overview
Controllers: Programming Application Logic - Part 2
internet.com site name
MS Access and MySQL
Cisco AutoQoS: VoIP QoS for Mere Mortals
While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags




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: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES