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 7

By Rob Gravelle

Digg This Add to del.icio.us

Applications Developer - Sharepoint
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume

In this series we've built a fully functional Autocomplete control within Eclipse using the Web Tools Project, JavaServer Pages, a Tomcat server, JavaScript, AJAX, JSON and some CSS. For the remaining installments, we'll be updating the Autocomplete control's layout to CSS, decoupling it from the Autocomplete.jsp page, tweaking the appearance of the list, and implementing additional functionality, including:

The New-and-Improved Autocomplete Control

This week, we convert the Autocomplete search control's layout from a table-based one to CSS and move it from the Autocomplete.jsp page into its own JSP file. While the table provided an easy means of laying out the control and offered the advantage of giving us a placeholder for the list, we're now going to trim some of the extra code out of our control so we can define its appearance from a CSS file. We're also going to cover a new way to create and position the list using JavaScript code.

The power of CSS is in separating page appearance specifications from the content. Removing superfluous formatting code and substituting CSS rules makes it easier to modify the appearance of the control since all the style rules will be in one place. Moreover, CSS layouts offer an endless variety of layout options that are all but impossible to achieve with a table.

Looking at the original table layout below, you can see how the presentation elements are mixed in with the HTML code. There are numerous formatting tags and attributes such as <B>, border="0, "cellpadding="5, "cellspacing="0, "align="left, " and even size="20" in the textbox all relate to the rendering of the elements in the browser. These will be relocated to the CSS file:

The AutocompleteCSS Project

Rather than rework our existing files, we'll start fresh and create a new project. Copying a whole project is a breeze in Eclipse. Simply select the Autocomplete project and click Ctrl+C to copy it and Ctrl+V to paste it. Eclipse even gives you a dialog to change the name from the default of "Copy of <project name>." We'll call this one "AutocompleteCSS." Hit the "OK" button to create the project (See Figure 1).

The AutocompleteControl JSP Page

To create the new Autocomplete control, use the Shiftt-Alt-N shortcut to open the "New" popup menu and select the "JSP" item (See Figure 2).

Enter "AutocompleteControl" in the "File Name" textbox and click the Next > button (See Figure 3).

Uncheck the "Use JSP Template" checkbox so a blank page will be loaded into the editor window (See Figure 4).

We can remove all table properties by using <DIV> tags for the various components, instead of table cells. Add the following code in the editor:

To make our control more autonomous, we should also move the external file references here. Include the following code at the top of the file, above the <form> tag:

The AutocompleteControl CSS File

We should rename the AutocompleteList.css file to AutocompleteControl.css to reflect that it will now contain rules for the entire control, not just the Autocomplete list. To rename the file, right-click on the AutocompleteList.css file in the "Project Explorer" pane to bring up the popup menu and select the "Rename" item. The file will then turn into a textbox for editing. Alternatively, you can press the F2 function key (See Figure 5)

What follows is an explanation of the new CSS rules:

The AutocompleteControl Selector

The most direct way to alter the appearance of the Autocomplete Control is by using ID selectors. We can switch to more generic selectors later should we wish to add more controls on the same page.

As we learned in Part 5: The AutocompleteList.css File, elements are positioned with static positioning by default, meaning that these are positioned within the normal flow of the page. While assigning a position of relative doesn't reposition the AutocompleteControl without the left and top properties defined, it does have the important effect of setting the AutocompleteControl <div> as the container for its child elements. Without it, they would be positioned with respect to the page's left and top properties.

The height and width properties replace the <table> height and width attributes. We didn't define these attributes originally because tables will expand by default to accommodate their contents. <div> elements might or might not, depending on how its child nodes are positioned. There's also an extra row in the original table that acted as a placeholder for the auto-row, where the list would appear. We won't be needing it in the CSS design:

The Autocomplete Class Selector

The Autocomplete class selector applies to all the elements contained within the Autocomplete control. All three elements, the label, the textbox and the submit button, are absolutely positioned with respect to the containing AutocompleteControl <div>. They're all aligned to the top of the container and have a height of 24 pixels. The line-height: 24px; rule has the effect of centering the elements vertically. The padding replaces the table's cellpadding attribute:

The AutocompleteTextFieldLabel ID Selector

This selector applies to the label <div> tag. With the top set at "0" pixels, we need only set the left and width properties for each element. The white-space property acts exactly the same as the table's NOWRAP attribute. The font-weight: bold; rule replaces the <B></B> tags:

The AutocompleteTextField ID Selector:

The AutocompleteTextField ID selector applies to the AutocompleteTextField <div>. All we need to set are its left and width properties:

The complete-field ID Selector:

The complete-field ID selector applies to the textbox. The width rule replaces the text input's size attribute:

The AutocompleteSubmit ID Selector:

The AutocompleteSubmit ID selector applies to the AutocompleteSubmit <div>. Again, we only need to set the left and width properties:


[next]

Recent Articles

WebReference.com site name
Controllers: Programming Application Logic - Part 2
How to Use JavaScript to Validate Form Data
Controllers: Programming Application Logic
internet.com site name
Sprint Launches Mobile WiMAX Network
Albatron Downsizes with the KI780G Mini-ITX Motherboard
Can't Find a Wi-Fi Network? Make Your Own.




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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES