Hour 14. Creating Advanced Web Page Elements


What You’ll Learn in This Hour:

Image Creating an image gallery with slider controls

Image Adding sorting to table elements

Image Adding filtering to table elements

Image How to create a multitype tree view

Image How to implement custom dialogs

Image Creating visual elements using basic HTML and jQuery

Image How to use jQuery to create dynamic sparklines


In this hour, you have some fun creating some more advanced page elements. The purpose of this hour is to give you a chance to apply everything that you have learned so far in this book in some more advanced ways. The elements you build in this hour include an image gallery, interactive table, tree view, overlays, equalizer, and sparklines.

The following sections are written as examples that first explain some of the concepts used in the advanced elements; then you step through implementing them yourself. You will be able to apply the concepts learned in this chapter to implement a variety of types of advanced elements in your own web pages.

Adding an Image Gallery

One of the most visually interactive components of web pages are image galleries. An image is truly worth a thousand words. Images allow users to make decisions faster than any other type of visual element. The best way to apply images in allowing users to make decisions is to provide an interactive gallery.

An interactive gallery should enable users to quickly scan through several images and then easily select one to lead to the next step on the web page. Therefore, the necessary components are image thumbnails and controls. The thumbnails should be small versions of the original image that allow you to provide several options to select from at a time. Controls are whatever you apply to navigate through the thumbnails.

The most effective method of implementing an image gallery is to create a slider that contains the thumbnails with arrows that slide through the thumbnails until the user finds and clicks the desired image. Sliders are a good choice because they can be implemented in vertical or horizontal fashion to fit the design of the web page.

To implement the slider, you create a container <div> element that has a fixed size and hidden overflow, then a child <div> element that contains all the thumbnail images. The position of the child <div> can then be adjusted to reveal different sets of the images in the slider. The following section describes that process.

Implementing Tables with Sorting and Filters

Tables are basic HTML. You should already be familiar with the concepts of adding tables with rows that include one or more <th> or <td> elements that provide column values. Tables are a great way to present data that can be organized into columns or rows.

For simple amounts of data, a basic static HTML table is adequate. But, what do you do if there is a lot of data in the table? Some web pages scroll on and on with tabular data. They are a pain to navigate, and in the end it’s easier to use the web browser search capability to try to find what you are looking for.

One solution to this is to implement sorting and filtering in server-side scripts via AJAX request. Occasionally that is the best method when dealing with extremely large amounts of data stored in a database or extremely complex filtering algorithms. However, for most web pages, it is overkill and requires a bunch of slow server requests that make the web page interaction disjointed.

A much better approach is to add filtering and sorting to those large tables directly in jQuery and JavaScript. That allows the user to filter the data down to a specific set and then order by columns very quickly. The page flow is much smoother, and no external server requests are required. The following section takes you through the process of implementing column sorting and filtering to a table.

Creating a Tree View

A tree view can be one of the most useful components when trying to present a large number of options to users. That is why tree views are used in so many places. Virtually all users will be familiar with them to at least a certain extent because it is the most common element used by OSes when displaying files and folders.

A tree view is simple to implement in jQuery and JavaScript. A very cool thing about tree views in your web pages is that they can contain any content that you want to display. The following section takes you through the process of implementing a tree view using jQuery and JavaScript.

Using Overlay Dialogs

You have already learned how to add several types of dialogs to your web pages using JavaScript. The problem is that the built-in dialogs are extremely limited in what you can do with them. You could open a new pop-up window to get more control, but pop-up windows have their own problems, the biggest of which is that users hate them and usually disable them in their browsers.

A much better option is to create your own dialog element using jQuery and JavaScript. Implementing a dialog requires two things: an overlay element that will mask off everything that is happening behind on the web pages and a dialog component that provides the dialog interaction.

The overlay needs to have a higher z-index than the main web page elements below. The overlay should also be somewhat transparent so that the user can see the web page in the background but is unable to click any of the web page elements under the overlay.

The dialog needs to have a higher z-index than the overlay so that it is visible when the overlay is displayed. The dialog also needs to have a means of closing, which makes the dialog and overlay elements hidden. You can include anything else in the dialog that you desire to provide user interaction.

The dialog and overlay need to be in a fixed position and initially hidden. When you want to display the dialog, unhide the overlay and dialog and then hide them when you are finished displaying the dialog. The following section takes you through that process.

Implementing a Graphical Equalizer Display

The total purpose of this section is to help you see a method of using basic HTML elements along with dynamic jQuery and JavaScript interactions with data to provide a rich user experience with a visual indicator of what is happening with data. The data could be coming from a variety of sources, including JavaScript running on the web page or external services collected by AJAX requests.

A graphic equalizer element provides a great way to view several values at once and is something that many users are already familiar with. The following sections walk you through the process of implementing a graphical equalizer.

Adding Sparkline Graphics

The purpose of this section is to help you see a method of using some of the new HTML5 elements along with dynamic jQuery and JavaScript interactions with data to provide a rich user experience with a visual indicator of trending data. The data could be coming from a variety of sources, including JavaScript running on the web page or external services collected by AJAX requests.

In this section, you implement a series of sparklines. A sparkline is a mini-graph that is updated frequently with the latest values from the web server. A sparkline element provides a great way for you to see how to use the new HTML elements to provide users with a great visual indicator of data trends. The following sections walk you through the process of implementing the sparkline.

Summary

In this hour, you got a chance to implement several more advanced web elements. You learned the basics of implementing image galleries, how to add sorting and filtering to a table, and how to dynamically create a tree view. You also got a chance to implement some graphical elements, such as sparklines and an equalizer display.

Q&A

Q. In your example, you populated the image slider using an array. Is that the best method?

A. Not necessarily. That was used for simplicity in the example. You can also use a static file located on the web server to get the list of files, or use an AJAX request to a server-side script that returns the list of images to display.

Q. Is it better to use a <canvas> or an <svg> element to draw chart type data such as the sparkline?

A. I used a <canvas> because it is simple to implement the changing lines. An <svg> chart would be too large to include in this book. The downside is that the canvas gets blurry if you zoom in on the web page. For the best charts, you should use <svg> so that the user can zoom in and it is still clean.

Workshop

The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try to answer the questions before looking at the answers.

Quiz

1. How do you use jQuery to move the slider in the image gallery?

2. Why do you use an overlay element along with the dialog when creating custom dialogs?

3. How do you make the dialog box appear on top of other elements?

Quiz Answers

1. The slider is really just a big div. To adjust the position, change the relative position using the .css() or .animate() methods.

2. The overlay keeps the user from clicking on the rest of the web page until the dialog is closed.

3. Set the z-index to a higher value.

Exercises

1. Modify the code in Listing 14.2 so that the filter for numerical columns will filter out items less than the value specified when the column is in ascending order and items greater than the value specified when the column is in descending order.

2. Modify the code in Listing 14.3 to add expand all and collapse all buttons. The buttons should expand or collapse all elements in the tree.

3. This exercise is for more advanced CSS and HTML users. In the code in Listing 14.1, modify the slider to be vertical. You will need to change positioning code to adjust the top instead of the left, move things around on the page, and do quite a number of CSS changes to get the slider to look right.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset