39,59 €
jQuery UI is the quintessential framework for creating professional user interfaces. While jQuery core lays the foundation for interaction with the DOM and handling events, jQuery UI fills in the user interaction gap. This book will give you a huge productivity boost out of the box with jQuery UI, and help you understand the framework, inside and out."jQuery UI Cookbook" provides you with practical recipes featuring in-depth coverage of every widget in the framework, including how to address limitations that impact your everyday development activities with these widgets. You'll get a better idea of the big picture – how the framework is composed, how the widgets relate to one another, and how to build on those patterns.Be it a minor tweak on the visual design of a progress bar or a fundamental change in a widget to meet your needs, "jQuery UI Cookbook" covers scenarios both big and small. You can show reminders as tooltips, apply a variety of effects to the menu widget, and start interactions between the dialog widget and API data using deferred objects. These and many more interesting tasks are covered in this book, which can be done with smooth learning and great understanding. You will see how button widgets can fill the width of their containing element, making the layout more consistent. Tabs can be sorted and moved between widgets. You will learn how to do all these things within the context of the big picture, by finding out why the components work the way they do, making you well-versed in jQuery UI.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 297
Veröffentlichungsjahr: 2013
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2013
Production Reference: 1120713
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-218-6
www.packtpub.com
Cover Image by Jarek Blaminsky (<[email protected]>)
Author
Adam Boduch
Reviewers
Hrishikesh Choudhari
Deepak Vohra
Acquisition Editor
Edward Gordon
Lead Technical Editor
Susmita Panda
Technical Editors
Shashank Desai
Worrell Lewis
Rikita Poojari
Amit Ramadas
Project Coordinator
Kranti Berde
Proofreaders
Lydia May Morris
Jonathan Todd
Indexer
Monica Ajmera Mehta
Production Coordinator
Aditi Gajjar
Cover Work
Aditi Gajjar
Adam Boduch has spent the last several years developing user interfaces for large-scale software systems. Starting out as a backend Python developer, he was lured to the frontend by tools like jQuery UI. Adam is passionate about readable code, and writes extensively about jQuery UI widgets, including his previous book jQuery UI Themes Beginner's Guide, Packt Publishing.
When Adam isn't coding, reading or writing, you will usually find him playing hockey, or spending time with his family.
Hrishikesh Choudhari has been developing single page rich applications using a host of client-side technologies. He has a special preference for JSON-emitting servers and delicious interfaces on the frontend. He has worked on the backend for innovative social networks.
He is a professional data visualization expert, and builds his own visualization micro libraries for SVG. He contributed to the book FusionCharts Beginner's Guide, Packt Publishing. He also helped design dashboards for clients ranging from Fortune 10 companies to startups.
He works on his skills to be a full stack web architect. He graduated magna cum laude in B.S. in Software Engineering from Champlain College, USA.
In his free time, he speed-reads, cooks, and goes for long walks. You can follow him on Twitter at @hchoudhari or on LinkedIn in.linkedin.com/in/hrishikeshchoudhari. His website can be found at http://hrishikeshchoudhari.com/.
Deepak Vohra is a consultant and a principal member of the NuBean.com software company. He is a Sun Certified Java Programmer and Web Component Developer, and has worked in the fields of XML and Java programming and J2EE for over five years. He is the co-author of the book Pro XML Development with Java Technology, Apress, and was the technical reviewer for the book WebLogic: The Definitive Guide, O'Reilly Media.
Deepak was also the technical reviewer for the book Ruby Programming for the Absolute Beginner, Course Technology PTR and the Technical Editor for the book Prototype and Scriptaculous in Action, Manning Publications. He is also the author of the books JDBC 4.0 and Oracle JDeveloper for J2EE Development, Processing XML Documents with Oracle JDeveloper 11g, and EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g, Packt Publishing.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
For Ted
Creating user experiences that excite users is a fun and rewarding job. You're essentially improving the lives of many people. Most UI developers have their eye on the finish line, seeing their product put to use. The faster we get to that finish line without sacrificing quality, the better. The tools we use to help get us there can make all the difference in the world.
Part of what makes the jQuery Framework so popular among developers, the "write less, do more" mantra, materializes in jQuery UI as well. The modern versions of HTML and CSS standards have the tools required for assembling a robust, responsive user interface. Where this idea falls apart—browser inconsistencies and lack of development conventions and patterns across projects—jQuery UI steps in. The goal of jQuery UI isn't to reinvent the way we write web applications, but rather, to fill in gaps and progressively enhance existing browser components.
Like any framework, jQuery UI isn't for everyone, nor is it perfect for those that do use it. The framework embraces this fact, and provides extensibility mechanisms for most situations you might find yourself in. My goal with this book is to share with you some experiences I've had with jQuery UI widgets. I've extended where possible, and hacked where necessary. I'm sure you'll find the majority of the recipes in this book useful, no matter what kind of application you're building.
Chapter 1, Creating Accordions, helps you learn how to drag-and-drop between accordion widgets. In addition, you'll learn how to extend the accordion theme.
Chapter 2, Including Autocompletes, explains the autocomplete widget that shows how to use multiple data sources. Turning select options into autocomplete widgets, and remote data source filtering are covered too.
Chapter 3, Crafting Buttons, explains about modifying buttons for our application. Buttons can be simple, modifying text and icon options. Or, buttons can be more involved, such as when working with button sets. We'll look into spacing issues, and how to apply effects.
Chapter 4, Developing Datepickers, talks about datepicker, which is the most widely-used widget, yet the most under-utilized. We'll uncover some potentials of the widget by using some techniques to better integrate datepicker into your application.
Chapter 5, Adding Dialogs, discusses dialog widgets, which often rely on API data. We'll look into loading data and dialog display issues. We also cover changing the dialog title bar, and applying effects to the widget.
Chapter 6, Making Menus, helps you learn how to make sortable menu items. We'll address theme concerns and highlighting the active menu item as well.
Chapter 7, Progress Bars, shows how to add labels to progress bars. We'll also extend the progress bar to make a loading widget.
Chapter 8, Using Sliders, talks about the slider widget that doesn't display step increments. Here, you will extend the widget to provide this capability. We also look into changing the visual display of the slider handle.
Chapter 9, Using Spinners, explains spinners, which are often used in forms. So we deal with formatting spinner values for local currencies and dates in this chapter. We'll also look into addressing theme concerns with the widget.
Chapter 10, Using Tabs, introduces some new techniques in working with tabs, that is, using each tab as a plain URL link. We also cover some more advanced tab navigation usage—dynamic loading and reading the browser hash.
Chapter 11, Using Tooltips, explains tooltips, which can be applied to just about anything on the page. In this chapter, we'll show you how to apply effects to the tooltip, change the tooltip state, and apply tooltips to selected text.
Chapter 12, Widgets and More!, talks about widgets, which don't exist in a vacuum. They're part of a larger application. This chapter covers the bigger jQuery UI development picture. This includes building a widget from scratch, building your own development tools, and working with Backbone.
You will require the following:
This book is for the jQuery UI developer looking to improve their existing applications, extract ideas for their new application, or to better understand the overall widget architecture. The reader should at least have a rudimentary understanding of what jQuery UI is, and have written some code that uses jQuery UI. The recipes in this book are targeted at the intermediate jQuery UI developer. Depending on your needs, each recipe is self-contained enough to be useful on its own, but connected enough to guide you to others.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "In this scenario, we're better off just changing the default dateFormat value to something our application uses throughout."
A block of code is set as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " Clicking on the no icons link would result in the button icons being removed, and replaced with their text."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, we will cover the following recipes:
In this chapter, we will explore the various ways in which we can extend the accordion widget to accommodate a number of scenarios. The accordion widget offers a lot of out-of-the-box functionality. For example, without any configuration, we get a themed container widget that groups contents into sections.
We'll focus on use cases that shed light on the inner workings of the accordion widget. Keyboard events are one way to navigate the page, and we can enhance the accordion's support for these events. Some magic happens behind the scene to come up with each section's height, when expanded. We'll see how we can work with these configurations, especially when the section height changes on the fly.
Also on the topic of height, we can let the user control the height of individual sections, or, from a theme perspective, we can control the space between the accordion components. Finally, we'll look at some of the more advanced accordion usage where we give the user the freedom to sort their accordion sections and to drag sections from one accordion to another.
Accordions are containers that are used to organize and display other UI elements. Thinking about each accordion section as static content is a mistake. The contents of accordion sections do change. For example, a user-triggered event might lead to the creation of a new element within the section. In all likelihood, the components inside a section will change size dynamically, and that's the part we need to be aware of. Why does it matter that accordion contents change size? Since this is an accordion, we'll likely have several sections (or at least a few). Does it make sense to have all of them with a uniform height? It does, until the height of one section grows too large. Then the section heights are no longer uniform. When this happens, we need to take a look at the accordion section height when they change, and potentially adjust some of the height settings on the fly.
Let's use the following markup to create an accordion widget:
We'll create the accordion using all the default option values as follows:
Now, this is where we'll notice a slight inconsistency with regards to height. Here is what the first section looks like. It has minimal content, but uses more space than required.
This is due to the default value of the heightStyle option, which says that the height of every section in the accordion will be equal to that of the tallest section. Thus, we have wasted space in the first section. Let's look at the fourth section in the following screenshot to see why this happens:
We can see that the first section is as tall as the fourth section. This is due to the auto value of heightStyle. In this particular example, the difference isn't all that great. That is, the first section doesn't waste too much empty space. Therefore, it would probably make sense to keep this accordion configuration where each section has the same height.
The challenge arises when we're dealing with an application that is dynamically feeding content into a particular accordion section, and at some point where a certain threshold is reached, it no longer makes sense to keep the auto heightStyle configuration.
Setting the heightStyle to auto solves the problem for us, as each section will only use the height necessary to display the content. However, it would be nice if we were able to change this property of the accordion when the height of the content itself changes.
What we've done here is extend the accordion widget's refresh() method to allow the heightStyleoption to be changed to content on the fly. The default implementation doesn't allow this. To illustrate this idea, consider the code above where we're creating the accordion widget and adding 20 new items to the last content section. We're using the default section height here, that is, auto. So, had we not extended the refresh() method to allow this behavior after populating the fourth section, we would have seen a scrollbar here.
Resizable content sections allow the user to adjust the height by dragging the bottom of the section. This is a nice alternative having to rely on the heightStyleproperty. Thus, if each section of the accordion can be adjusted by the user, they have the freedom to tailor the accordion layout. For example, if the accordion has a tall section, with wasted space at the bottom, the user might choose to shrink the height of that section to gain a better view of the accordion, and other components of the UI for that matter.
We'll extend the default accordion's _create() method by making each content's div within the accordion resizable using the resizable interaction widget.
You'll see something similar to the following. Notice that the second section has been dragged down and has the resize mouse cursor.
Our new version of the _create()method works by first invoking the default accordion's _create() method. Once that completes, we find all content sections of the accordion and apply the resizable() widget. You'll notice, too, that we've told the resizable widget to only show a south handle. This means that the user will only be able to drag any given content section of the accordion up or down, using the cursor at the bottom of the section.
This specialization of an accordion also provides a new implementation of the _delete() method. Once again, we're calling the original accordion's _delete(), after which we're cleaning up the new resizable components we added. This includes removing the overflow CSS property.
We can extend our resizable behavior within the accordion by providing a means to turn it off. We'll add a simple resizable option to the accordion that checks whether or not to make the accordion sections resizable.
The space between accordion sections is controlled by the CSS theme framework. In particular, the visual structure for the accordion is defined by a set of CSS rules that can be modified to control the spacing between accordion sections. We could override the accordion theme CSS to adjust for more or less spacing between sections.
We're going to supply an additional CSS module to our UI—one that overrides the accordion structure supplied in the theme we happen to be using. There's no need to fret, however, our change is a simple one. We're going to update the margin-top property. In a new CSS file called theme.accordion.css, let's add the following style rules:
Now that we have the CSS, we need to include it in our HTML header. It should look something like this:
We're copying the same CSS selector as is found in any jQuery UI theme. The particular property we've just changed alters the space between the accordion sections. Since we're overriding the default theme value, it's important to include our CSS files after the default theme file. This allows us to override the default theme instead of the default theme overriding our modifications.
Using the sortable interaction widget, we're able to transform a static accordion section layout into something specified by the user. That is, sortable interaction widgets take a container element, and allow all child elements to be sorted in place. The user does this by dragging the element to the desired order.
We'll look at how we can extend the accordion capabilities so that the sortable section functionality is encapsulated, and can be switched on by a configuration option at the time of creation.
We have to perform several actions when the accordion widget is created, and when the accordion is destroyed. Here is how we extend the widget:
With our new accordion widget marked as sortable, users now have the ability to drag header sections around within the accordion. For instance, if the first accordion section belongs to the bottom, the user just drags it to the bottom.
With the help of the sortable() interaction widget, we're able to extend the default accordion widget implementation to include sorting capabilities. As with any jQuery UI widget enhancements, we don't actually need to extend the widget in question; the new capabilities can always be tacked-on after the widget has been instantiated. However, as you'll see throughout this book, the best practice is to encapsulate customizations and present them to the widget client as a set of options.
Here, we've extended the set of available accordion options to include a sortable