Advanced JQuery Mobile tutorial with Phonegap

Post on 02-Jul-2015

694 views 4 download

description

Introduction to jQuery Mobile (jQM) - cont'd Getting started with jQM -Downloading the Most Recent Version of jQuery Mobile -Proper Markup for Loading Framework JavaScript and CSS jQuery Mobile Page Structure -Page Anatomy: Header, Footer and Content Sections -Header and Footer Toolbars -Bundling Pages into a Single Document -Navigating Between Pages Applying Different Theme Swatches Page Initialization Events jQuery Mobile Page Components Basic Content Formatting List Views -Ordered and Unordered Lists -Inset Lists -Lists with Links -Nested Lists -Lists with Icons or Thumbnail Images -Split Button Lists -List Dividers -Search Filters Form Controls - check boxes, slider, etc. Dialogs Buttons and Toolbars -Ways to Make a Button -Placing Icons on Your Buttons -Inline Buttons -Button Groupings -Navigation Toolbars Collapsible Content Event Handling -Responding to various events -Page related events Ajax & Interaction with server (REST & SOAP) Deployment using Phonegap (e.g. Android) Overview of Android Devt Environment Best Practices in jQM Hands-on exercises

Transcript of Advanced JQuery Mobile tutorial with Phonegap

Introduction to JQuery Mobile

Rakesh Kumar Jha

M.Tech, MBA

Contents Introduction to jQuery Mobile (jQM) Getting started with jQM

-Downloading the Most Recent Version of jQuery Mobile -Proper Markup for Loading Framework JavaScript and CSS

jQuery Mobile Page Structure

-Page Anatomy: Header, Footer and Content Sections -Header and Footer Toolbars

-Bundling Pages into a Single Document -Navigating Between Pages

Applying Different Theme Swatches Page Initialization Events jQuery Mobile Page Components

Basic Content Formatting List Views

-Ordered and Unordered Lists -Inset Lists

-Lists with Links -Nested Lists -Lists with Icons or Thumbnail Images

-Split Button Lists -List Dividers

-Search Filters Form Controls - check boxes, slider, etc.

Introduction to jQuery Mobile (jQM)

• jQuery is a JavaScript Library.

• jQuery greatly simplifies JavaScript programming.

• jQuery is easy to learn

Introduction to jQuery Mobile (jQM)

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").click(function(){ $(this).hide(); }); }); </script> </head> <body> <p>If you click on me, I will disappear.</p> <p>Click me away!</p> <p>Click me too!</p> </body> </html>

jQuery Introduction

• The purpose of jQuery is to make it much easier to use JavaScript on your website.

What is jQuery

• jQuery is a lightweight, "write less, do more", JavaScript library.

• The purpose of jQuery is to make it much easier to use JavaScript on your website.

What is jQuery

• jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code

What is jQuery

• The jQuery library contains the following features:

– HTML/DOM manipulation

– CSS manipulation

– HTML event methods

– Effects and animations

– AJAX

– Utilities

Why jQuery

• The jQuery library contains the following features:

– Easy to use

– Less LOC

– Simplify code

– Rich api

jQuery Install

• There are several ways to start using jQuery on your web site. You can:

– Download the jQuery library from jQuery.com

– Include jQuery from a CDN, like Google

Downloading jQuery

• There are two versions of jQuery available for downloading:

– Production version - this is for your live website because it has been minified and compressed

– Development version - this is for testing and development (uncompressed and readable code)

http://jquery.com/download/

Downloading jQuery

• The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section):

<head> <script src="jquery-1.11.1.min.js"></script> </head>

jQuery CDN

• If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery Network).

<head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head>

jQuery CDN

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

jQuery Syntax

• With jQuery you select (query) HTML elements and perform "actions" on them.

• The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).

jQuery Syntax

• Basic syntax is: $(selector).action()

– A $ sign to define/access jQuery

– A (selector) to "query (or find)" HTML elements

– A jQuery action() to be performed on the element(s)

jQuery Syntax

• Examples:

– $(this).hide() - hides the current element.

– $("p").hide() - hides all <p> elements.

– $(".test").hide() - hides all elements with class="test".

– $("#test").hide() - hides the element with id="test".

The Document Ready Event

$(document).ready(function()

{ // jQuery methods go here... });

This is to prevent any jQuery code from running before the document is finished loading (is ready).

The Document Ready Event

• It is good practice to wait for the document to be fully loaded and ready before working with it.

• This also allows you to have your JavaScript code before the body of your document, in the head section.

The Document Ready Event

• Here are some examples of actions that can fail if methods are run before the document is fully loaded:

– Trying to hide an element that is not created yet

– Trying to get the size of an image that is not loaded yet

The Document Ready Event

• The jQuery team has also created an even shorter method for the document ready event:

$(function(){ // jQuery methods go here... });

jQuery Selectors

The element Selector

• All selectors in jQuery start with the dollar sign and parentheses: $().

• The jQuery element selector selects elements based on the element name.

• You can select all <p> elements on a page like this:

jQuery selectors

• jQuery selectors are one of the most important parts of the jQuery library

• jQuery selectors allow you to select and manipulate HTML element(s)

• jQuery selectors are used to "find" (or select) HTML elements based on their id, classes, types, attributes, values of attributes

jQuery selectors

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

The #id Selector

• The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

• An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

The #id Selector

• To find an element with a specific id, write a hash character, followed by the id of the HTML element:

$("#test")

The #id Selector

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#test").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p id="test">This is another paragraph.</p> <button>Click me</button> </body> </html>

The .class Selector

• The jQuery class selector finds elements with a specific class.

• To find elements with a specific class, write a period character, followed by the name of the class:

$(".test")

The .class Selector

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $(".test").hide(); }); }); </script> </head> <body> <h2 class="test">This is a heading</h2> <p class="test">This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

The .class Selector

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("*").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

The .class Selector

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $(this).hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

The .class Selector

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p.intro").hide(); }); }); </script> </head> <body> <h2 class="intro">This is a heading</h2> <p class="intro">This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>

The .class Selector <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("ul li:first").hide(); }); }); </script> </head> <body> <p>List 1:</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Tea</li> </ul> <p>List 2:</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Tea</li> </ul> <button>Click me</button> </body> </html>

The .class Selector <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("ul li:first-child").hide(); }); }); </script> </head> <body> <p>List 1:</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Tea</li> </ul> <p>List 2:</p> <ul> <li>Coffee</li> <li>Milk</li> <li>Tea</li> </ul> <button>Click me</button> </body> </html>

Functions In a Separate File

• If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQuery functions in a separate .js file.

<head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script src="my_jquery_functions.js"></script> </head>

jQuery Event Methods

jQuery Event Methods

• jQuery is tailor-made to respond to events in an HTML page.

What are Events?

• All the different visitor's actions that a web page can respond to are called events.

• An event represents the precise moment when something happens.

• Examples:

– moving a mouse over an element

– selecting a radio button

– clicking on an element

What are Events?

Mouse Events Keyboard Events

Form Events Document/Window Events

click keypress submit load

dblclick keydown change resize

mouseenter keyup focus scroll

mouseleave blur unload

jQuery Syntax For Event Methods

• In jQuery, most DOM events have an equivalent jQuery method.

• To assign a click event to all paragraphs on a page, you can do this:

jQuery Syntax For Event Methods

$("p").click();

• The next step is to define what should happen when the event fires. You must pass a function to the event:

$("p").click(function(){ // action goes here!! });

Commonly Used jQuery Event Methods

• $(document).ready() • click() • dblclick() • mouseenter() • mouseleave() • mousedown() • mouseup() • hover() • focus() • blur()

<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").dblclick(function(){ $(this).hide(); }); }); </script> </head> <body> <p>If you double-click on me, I will disappear.</p> <p>Click me away!</p> <p>Click me too!</p> </body> </html>

jQuery Mobile Page Structure

Page Anatomy: Header, Footer and Content Sections

Header and Footer Toolbars

• Toolbar elements are often placed inside headers and footers - for "easy-access" navigation:

Header Bars

• The header is located at the top of the page and usually contain a page title/logo or one or two buttons (typically home, options or search).

• You can add buttons to the left and/or to the right side in the header.

Header Bars <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

</head>

<body>

<div data-role="page">

<div data-role="header">

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">Home</a>

<h1>Welcome To My Homepage</h1>

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-search ui-btn-icon-left">Search</a>

</div>

<div data-role="main" class="ui-content">

<p>Notice that we have added the CSS class of "ui-corner-all" and "ui-shadow" to the header buttons, to make them look a bit more desirable.</p>

</div>

</div>

</body>

</html>

Header Bars <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

</head>

<body>

<div data-role="header">

<a href="#" class="ui-btn ui-btn-left ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">Home</a>

<h1>Welcome To My Homepage</h1>

</div>

</body>

</html>

Header Bars <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

</head>

<body>

<div data-role="header">

<h1>Welcome To My Homepage</h1>

<a href="#" class="ui-btn ui-btn-right ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">Search</a>

</div>

</body>

</html>

Footer Bars

• The footer is located at the bottom of the page.

• The footer is more flexible than the header - it is more functional and changeable throughout pages, and can therefore contain as many buttons as needed:

• The buttons in the footer are not centered by default. To fix this, simply use CSS:

<div data-role="footer" style="text-align:center;">

Footer Bars <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

</head>

<body>

<div data-role="page">

<div data-role="header">

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left ">Home</a>

<h1>Welcome To My Homepage</h1>

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-search ui-btn-icon-left ">Search</a>

</div>

<div data-role="main" class="ui-content">

<p>The buttons are for demonstration purposes only, and will not have any effect.</p>

<p>Notice that we have applied the CSS class of "ui-corner-all" and "ui-shadow" to the buttons in both the header and footer, to make them look more desirable.</p>

</div>

<div data-role="footer">

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Facebook</a>

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Twitter</a>

<a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">Add Me On Instagram</a>

</div>

</div>

</body>

</html>

Positioning Headers and Footers

• The header and the footer can be positioned in three ways:

– Inline - Default. Headers and footers are inline with the page content

– Fixed - Headers and footers will remain positioned at the top and bottom of the page

– Fullscreen - Behaves like fixed; headers and footers will remain positioned at the top and bottom, but also over the page content. It is also slightly see-through

Positioning Headers and Footers <!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

</head>

<body>

<div data-role="page">

<div data-role="header" data-position="fixed">

<h1>Inline Header</h1>

</div>

<div data-role="main" class="ui-content">

<p><b>Tip:</b> To see the effect, try to resize the window if the scrollbar is not available.</p>

<p>Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..</p>

</div>

<div data-role="footer" data-position="inline">

<h1>Inline Footer</h1>

</div>

</div>

</body>

</html>

Navigating Between Pages

• A navigation bar consists of a group of links that are aligned horizontally, typically within a header or footer

• The bar is coded as an unordered list of links wrapped inside a <div> element that has the data-role="navbar" attribute

Navigating Between Pages <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> <div data-role="navbar"> <ul> <li><a href="#">Home</a></li> <li><a href="#">Page Two</a></li> <li><a href="#">Search</a></li> </ul> </div> </div> <div data-role="main" class="ui-content"> <p>My Content..</p> </div> <div data-role="footer"> <h1>My Footer</h1> </div> </div> </body> </html>

Icons in Navigation Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> <div data-role="navbar"> <ul> <li><a href="#" data-icon="home">Home</a></li> <li><a href="#" data-icon="arrow-r">Page Two</a></li> <li><a href="#" data-icon="search">Search</a></li> </ul> </div> </div> <div data-role="main" class="ui-content"> <p>My Content..</p> </div> <div data-role="footer"> <h1>My Footer</h1> </div> </div> </body> </html>

Active Buttons

• When a link in the navbar is tapped/clicked, it gets the selected (pressed down) look.

• To achieve this look without having to tap the link, use the class="ui-btn-active":

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> <div data-role="navbar"> <ul> <li><a href="#" class="ui-btn-active" data-icon="home">Home</a></li> <li><a href="#pagetwo" data-icon="arrow-r">Page Two</a></li> </ul> </div> </div> <div data-role="main" class="ui-content"> <p>With the ui-btn-active class, notice that the Home button stays highlighted (selected).</p> <p>If you click on the Page Two, you will notice that none of the buttons are highlighted (selected).</p> </div> <div data-role="footer"> <h1>My Footer</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Welcome To My Homepage</h1> <div data-role="navbar"> <ul> <li><a href="#pageone" data-icon="home">Home</a></li> <li><a href="#" data-icon="arrow-r">Page Two</a></li> </ul> </div> </div> <div data-role="main" class="ui-content"> <p>No buttons are pre-selected (highlighted) in this page..</p> <p>To get the selected look for each button that represents the page you are actually on, go back to our navbar tutorial and read the next step to find out how!</p> </div> <div data-role="footer"> <h1>My Footer</h1> </div> </div> </body> </html>

Applying Different Theme Swatches

Applying Different Theme Swatches

• jQuery Mobile provides two different style themes, "a" and "b" - each with different colors for buttons, bars, content blocks, and so on.

Applying Different Theme Swatches

• To customize the look of your application, use the data-theme attribute, and assign the attribute with a letter:

<div data-role="page" data-theme="a|b">

Theme a <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone" data-theme="a"> <div data-role="header"> <h1>Page Header</h1> </div> <div data-role="main" class="ui-content"> <p>Some Text..</p> <a href="#">A Standard Text Link</a> <a href="#" class="ui-btn">Link Button</a> <p>A List View:</p> <ul data-role="listview" data-autodividers="true" data-inset="true"> <li><a href="#">Adele</a></li> <li><a href="#">Billy</a></li> </ul> <label for="fullname">Input Field:</label> <input type="text" name="fullname" id="fullname" placeholder="Name.."> <label for="switch">Toggle Switch:</label> <select name="switch" id="switch" data-role="slider"> <option value="on">On</option> <option value="off" selected>Off</option> </select> </div> <div data-role="footer"> <h1>Page Footer</h1> </div> </div> </body> </html>

Theme a <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone" data-theme="b"> <div data-role="header"> <h1>Page Header</h1> </div> <div data-role="main" class="ui-content"> <p>Some Text..</p> <a href="#">A Standard Text Link</a> <a href="#" class="ui-btn">Link Button</a> <p>A List View:</p> <ul data-role="listview" data-autodividers="true" data-inset="true"> <li><a href="#">Adele</a></li> <li><a href="#">Billy</a></li> </ul> <label for="fullname">Input Field:</label> <input type="text" name="fullname" id="fullname" placeholder="Name.."> <label for="switch">Toggle Switch:</label> <select name="switch" id="switch" data-role="slider"> <option value="on">On</option> <option value="off" selected>Off</option> </select> </div> <div data-role="footer"> <h1>Page Footer</h1> </div> </div> </body> </html>

jQuery Mobile Pages

jQuery Mobile Pages

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>I Am Now A Mobile Developer!!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Pages

• The data-role="page" is the page displayed in the browser

• The data-role="header" creates a toolbar at the top of the page (often used for title or search buttons)

• The data-role="main" defines the content of the page, like text, images, buttons, forms, etc.

• The "ui-content" class adds extra padding and margin inside the page content

• The data-role="footer" creates a toolbar at the bottom of the page

• Inside these containers, you can add any HTML elements - paragraphs, images, headings, lists, etc.

Adding Pages in jQuery Mobile

• In jQuery Mobile, you can create multiple pages in a single HTML file.

• Separate each page with a unique id and use the href attribute to link between them:

Adding Pages in jQuery Mobile <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>Welcome! If you click on the link below, it will take you to Page Two.</p> <a href="#pagetwo">Go to Page Two</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>This is Page Two. If you click on the link below, it will take you to Page One.</p> <a href="#pageone">Go to Page One</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Using Pages as Dialogs

• A dialog box is a type of window used to show special information or request input.

• To create a dialog box that opens when a user taps on a link, add data-dialog="true" to the page you want displayed as a dialog:

Using Pages as Dialogs <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>Welcome!</p> <a href="#pagetwo">Go to Dialog Page</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" data-dialog="true" id="pagetwo"> <div data-role="header"> <h1>I'm A Dialog Box!</h1> </div> <div data-role="main" class="ui-content"> <p>The dialog box is different from a normal page, it is displayed on top of the current page and it will not span the entire width of the page. The dialog has also an icon of "X" in the header to close the box.</p> <a href="#pageone">Go to Page One</a> </div> <div data-role="footer"> <h1>Footer Text In Dialog</h1> </div> </div> </body> </html>

List Views

jQuery Mobile List Views

• List views in jQuery Mobile are standard HTML lists; ordered (<ol>) and unordered (<ul>).

• To create a list, apply the data-role="listview" to the <ol> or <ul> element. To make the items tappable, specify a link inside each list item (<li>):

jQuery Mobile List Views

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>Ordered List:</h2> <ol data-role="listview"> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> </ol> <h2>Unordered List:</h2> <ul data-role="listview"> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> </ul> </div> </div> </body> </html>

jQuery Mobile List Views

• To style your lists with rounded corners and some margin, use the data-inset="true" attribute:

jQuery Mobile List Views

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>A standard list:</h2> <ul data-role="listview"> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> </ul><br> <h2>List with data-inset="true":</h2> <ul data-role="listview" data-inset="true"> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> <li><a href="#">List Item</a></li> </ul> </div> </div> </body> </html>

List Dividers

• List dividers are used to organize and group items into categories/sections.

• To specify a list divider, add the data-role="list-divider" attribute to an <li> element:

List Dividers

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>List Dividers</h2> <ul data-role="listview" data-inset="true"> <li data-role="list-divider">Europe</li> <li><a href="#">Norway</a></li> <li><a href="#">Germany</a></li> <li data-role="list-divider">Asia</li> <li><a href="#">India</a></li> <li><a href="#">Thailand</a></li> <li data-role="list-divider">Africa</li> <li><a href="#">Zimbabwe</a></li> <li><a href="#">Uganda</a></li> </ul> </div> </div> </body> </html>

List Dividers

• If you have an alphabetically list, (for example a phone book) jQuery Mobile automatically adds appropriate dividers by setting the data-autodividers="true" attribute on the <ol> or <ul> element:

List Dividers <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>My Phonebook</h2> <ul data-role="listview" data-autodividers="true" data-inset="true"> <li><a href="#">Adele</a></li> <li><a href="#">Agnes</a></li> <li><a href="#">Albert</a></li> <li><a href="#">Billy</a></li> <li><a href="#">Bob</a></li> <li><a href="#">Calvin</a></li> <li><a href="#">Cameron</a></li> <li><a href="#">Chloe</a></li> <li><a href="#">Christina</a></li> <li><a href="#">Diana</a></li> <li><a href="#">Gabriel</a></li> <li><a href="#">Glen</a></li> <li><a href="#">Ralph</a></li> <li><a href="#">Valarie</a></li> </ul> <p>The data-autodividers="true" attribute creates dividers where it is appropriate with uppercased first letters of the item's text.</p> </div> </div> </body> </html>

jQuery Mobile List Content

jQuery Mobile List Icons

• The default icon for each list item containing a link is "carat-r" (right arrow).

• To change this to another icon, use the data-icon attribute on the list item you want to modify:

jQuery Mobile List Icons

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>List Icons:</h2> <ul data-role="listview" data-inset="true"> <li><a href="#">Default is right arrow</a></li> <li data-icon="plus"><a href="#">data-icon="plus"</a></li> <li data-icon="minus"><a href="#">data-icon="minus"</a></li> <li data-icon="delete"><a href="#">data-icon="delete"</a></li> <li data-icon="location"><a href="#">data-icon="location"</a></li> <li data-icon="false"><a href="#">data-icon="false"</a></li> </ul> </div> </div> </body> </html>

jQuery Mobile List Icons

data-icon="false" will remove the icon

• To add a standard 16x16px icon to your list, add an <img> element inside the link with a class of "ui-li-icon":

jQuery Mobile List Icons

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>List With Icons:</h2> <ul data-role="listview" data-inset="true"> <li><a href="#"><img src="us.png" alt="USA" class="ui-li-icon">USA</a></li> <li><a href="#"><img src="gb.png" alt="Great Britain" class="ui-li-icon">Great Britain</a></li> <li><a href="#"><img src="finland.png" alt="Finland" class="ui-li-icon">Finland</a></li> <li><a href="#"><img src="germany.png" alt="Germany" class="ui-li-icon">Germany</a></li> <li><a href="#"><img src="france.png" alt="France" class="ui-li-icon">France</a></li> </ul> </div> </div> </body> </html>

jQuery Mobile List Thumbnails

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>List With Icons:</h2> <ul data-role="listview" data-inset="true"> <li><a href="#"><img src="us.png" alt="USA" class="ui-li-icon">USA</a></li> <li><a href="#"><img src="gb.png" alt="Great Britain" class="ui-li-icon">Great Britain</a></li> <li><a href="#"><img src="finland.png" alt="Finland" class="ui-li-icon">Finland</a></li> <li><a href="#"><img src="germany.png" alt="Germany" class="ui-li-icon">Germany</a></li> <li><a href="#"><img src="france.png" alt="France" class="ui-li-icon">France</a></li> </ul> </div> </div> </body> </html>

Day 8

Dialogs Buttons and Toolbars -Ways to Make a Button -Placing Icons on Your Buttons -Inline Buttons -Button Groupings -Navigation Toolbars Collapsible Content Event Handling -Responding to various events -Page related events Deployment using Phonegap (e.g. Android) Overview of Android Devt Environment Best Practices in jQM Hands-on exercises

Split Buttons

• To create a split list with a vertical divider bar, place two links inside the <li> element.

• jQuery Mobile will automatically place the second link on the right side of the list with a right arrow-icon. And the text inside the link (if any) will be shown when a user hover over the icon:

Split Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>Split Buttons</h2> <ul data-role="listview" data-inset="true"> <li> <a href="#"> <img src="chrome.png"> <h2>Google Chrome</h2> <p>Google Chrome is a free, open-source web browser. Released in 2008.</p> </a> <a href="#">Some Text</a> </li> <li> <a href="#"> <img src="firefox.png"> <h2>Mozilla Firefox</h2> <p>Firefox is a web browser from Mozilla. Released in 2004.</p> </a> <a href="#">Some Text</a> </li> </ul> </div> </div> </body> </html>

Count Bubbles

• Count bubbles are used to display numbers associated with list items, such as messages in a mailbox:

Count Bubbles

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="main" class="ui-content"> <h2>My Email Account</h2> <ul data-role="listview" data-inset="true"> <li><a href="#">Inbox<span class="ui-li-count">25</span></a></li> <li><a href="#">Sent<span class="ui-li-count">432</span></a></li> <li><a href="#">Trash<span class="ui-li-count">7</span></a></li> </ul> </div> </div> </body> </html>

Placing Icons on Your Buttons

• Mobile applications are built upon the simplicity of tapping things you'd want displayed

Creating a Button in jQuery Mobile

• A button in jQuery Mobile can be created in three ways:

– Using the <input> element

– Using the <button> element with class="ui-btn"

– Using the <a> element with class="ui-btn"

Creating a Button in jQuery Mobile

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Buttons</h1> </div> <div data-role="main" class="ui-content"> <input type="button" value="Button"> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Navigation Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Buttons</h1> </div> <div data-role="main" class="ui-content"> <p>Welcome!</p> <a href="#pagetwo" class="ui-btn">Go to Page Two</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Buttons</h1> </div> <div data-role="main" class="ui-content"> <p>Goodbye!</p> <a href="#pageone" class="ui-btn">Go to Page One</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Grouped Buttons

• jQuery Mobile provides an easy way for grouping buttons together.

• Use the data-role="controlgroup" attribute together with data-type="horizontal|vertical" in a container element, to specify whether to group buttons horizontally or vertically:

Grouped Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Grouped Buttons</h1> </div> <div data-role="main" class="ui-content"> <div data-role="controlgroup" data-type="horizontal"> <p>Horizontal group:</p> <a href="#" class="ui-btn">Button 1</a> <a href="#" class="ui-btn">Button 2</a> <a href="#" class="ui-btn">Button 3</a> </div><br> <div data-role="controlgroup" data-type="vertical"> <p>Vertical group (default):</p> <a href="#" class="ui-btn">Button 1</a> <a href="#" class="ui-btn">Button 2</a> <a href="#" class="ui-btn">Button 3</a> </div> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Back Buttons

• To create a Back button, use the data-rel="back" attribute (Note: this will ignore the anchor's href value)

Back Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Back Button Example</h1> </div> <div data-role="main" class="ui-content"> <a href="#pagetwo" class="ui-btn">Go to Page Two</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Back Button Example</h1> </div> <div data-role="main" class="ui-content"> <a href="#" class="ui-btn" data-rel="back">Go Back</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Inline Buttons

• By default, buttons take up the full width of the screen.

• If you want a button that is only as wide as its content, or if you want two or more buttons to appear side by side, add the "ui-btn-inline" class:

Inline Buttons <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>Normal / Default button:</p> <a href="#pagetwo" class="ui-btn">Go to Page Two</a> <p>Inline button:</p> <a href="#pagetwo" class="ui-btn ui-btn-inline">Go to Page Two</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Welcome To My Homepage</h1> </div> <div data-role="main" class="ui-content"> <p>Inline buttons (will appear side by side as long as the screen lets it):</p> <a href="#pageone" class="ui-btn ui-btn-inline">Go to Page One</a> <a href="#pageone" class="ui-btn ui-btn-inline">Go to Page One</a> <a href="#pageone" class="ui-btn ui-btn-inline">Go to Page One</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Collapsible Content

Collapsible Content

• Collapsibles allow you to hide or show content, which is useful for storing parts of information.

Collapsible Content

• Collapsibles allow you to hide or show content, which is useful for storing parts of information.

• To create a collapsible block of content, assign the data-role="collapsible" attribute to a container. Inside the container (div), add a header (H1-H6) or legend element, followed by any HTML markup you want to be expanded:

Collapsible Content

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Collapsible Blocks</h1> </div> <div data-role="main" class="ui-content"> <div data-role="collapsible"> <h1>Click me - I'm collapsible!</h1> <p>I'm the expanded content.</p> </div> </div> <div data-role="footer"> <h1>Insert Footer Text Here</h1> </div> </div> </body> </html>

Nested Collapsible Blocks

• Collapsible content blocks can be nested (a collapsible inside a collapsible):

Nested Collapsible Blocks <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Nested Collapsible Blocks</h1> </div> <div data-role="main" class="ui-content"> <div data-role="collapsible"> <h1>Click me - I'm collapsible!</h1> <p>I'm the expanded content.</p> <div data-role="collapsible"> <h1>Click me - I'm a nested collapsible block!</h1> <p>I'm the expanded content in the nested collapsible block.</p> </div> </div> </div> <div data-role="footer"> <h1>Insert Footer Text Here</h1> </div> </div> </body> </html>

Collapsible Sets

• Collapsible sets are collapsible blocks that are grouped together (often referred to as an accordion). When a new block is opened, all other blocks close.

• Create several collapsible content blocks, and then wrap a new container with the data-role="collapsibleset" around the collapsible blocks:

Collapsible Sets <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Collapsible Sets</h1> </div> <div data-role="main" class="ui-content"> <div data-role="collapsibleset"> <div data-role="collapsible"> <h3>Click me - I'm collapsible!</h3> <p>I'm the expanded content.</p> </div> <div data-role="collapsible"> <h3>Click me - I'm collapsible!</h3> <p>I'm the expanded content.</p> </div> <div data-role="collapsible"> <h3>Click me - I'm collapsible!</h3> <p>I'm the expanded content.</p> </div> <div data-role="collapsible"> <h3>Click me - I'm collapsible!</h3> <p>I'm the expanded content.</p> </div> </div> </div> <div data-role="footer"> <h1>Insert Footer Text Here</h1> </div> </div> </body> </html>

Event Handling • Events = All the different visitor's actions that

a web page can respond to.

jQuery Mobile Events • Events = All the different visitor's actions that

a web page can respond to.

jQuery Mobile Events • You can use any standard jQuery events in jQuery

Mobile. • In addition, jQuery Mobile also offers several

events that are tailor-made for mobile-browsing: – Touch events - triggers when a user touches the

screen (tap and swipe) – Scroll events - triggers when a user scrolls up and

down – Orientation events - triggers when the device rotates

vertically or horizontally – Page events - triggers when a page is shown, hidden,

created, loaded and/or unloaded

Initializing jQuery Mobile Events • In jQuery, you have learned to use the

document ready event to prevent any jQuery code from running before the document is finished loading (is ready):

Initializing jQuery Mobile Events <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <p>If you click on me, I will disappear.</p> <p>Click me away!</p> <p>Click me too!</p> </body> </html>

Initializing jQuery Mobile Events • However, in jQuery Mobile, we use the

pagecreate event, which occurs when the page has been been created in the DOM, but before enhancement is complete.

• The second parameter ("#pageone") points to the id of the page to specify the event(s) for:

Initializing jQuery Mobile Events <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("click",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Header Text</h1> </div> <div data-role="main" class="ui-content"> <p>If you click on me, I will disappear.</p> <p>Click me away!</p> <p>Click me too!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Touch Events • Touch events are triggered when the user

touches the screen (page).

jQuery Mobile Tap • The tap event is triggered when the user taps

on an element.

• The following example says: When a tap event fires on a <p> element; hide the current <p> element:

jQuery Mobile Tap <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("tap",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The tap Event</h1> </div> <div data-role="main" class="ui-content"> <p>If you tap me, I will disappear.</p> <p>Tap me away!</p> <p>Tap me too!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Taphold • The taphold event is triggered when the user

taps on an element and hold for one second:

jQuery Mobile Taphold <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("taphold",function(){ $(this).hide(); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The taphold Event</h1> </div> <div data-role="main" class="ui-content"> <p>If you tap and hold me for one second, I will disappear.</p> <p>Tap and hold me!</p> <p>Tap and hold me too!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Swipe • The swipe event is triggered when the user

swipes over an element horizontally by more than 30px:

jQuery Mobile Swipe <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("swipe",function(){ $("span").text("Swipe detected!"); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The swipe Event</h1> </div> <div data-role="main" class="ui-content"> <p>Swipe this text or in the box below.</p> <p style="border:1px solid black;height:200px;width:200px;"></p> <p><span style="color:red"></span></p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Swipeleft • The swipeleft event is triggered when the user

swipes over an element in the left direction by more than 30px:

jQuery Mobile Swipeleft <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("swipeleft",function(){ alert("You swiped left!"); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The swipeleft Event</h1> </div> <div data-role="main" class="ui-content"> <p style="border:1px solid black;margin:5px;">Swipe me in the left direction - do not swipe outside the border!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Swiperight • The swiperight event is triggered when the

user drags over an element in the right direction by more than 30px:

jQuery Mobile Swiperight <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $("p").on("swiperight",function(){ alert("You swiped right!"); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The swiperight Event</h1> </div> <div data-role="main" class="ui-content"> <p style="border:1px solid black;margin:5px;">Swipe me in the right direction - do not swipe outside the border!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Scroll Events • jQuery Mobile provides two scroll events:

when scrolling starts and when scrolling stops.

jQuery Mobile Scrollstart • The scrollstart event is triggered when the

user starts to scroll the page:

jQuery Mobile Scrollstart <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate","#pageone",function(){ $(document).on("scrollstart",function(){ alert("Started scrolling!"); }); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>The scrollstart Event</h1> </div> <div data-role="main" class="ui-content"> <p><b>Tip:</b> Try to to resize the window if the scrollbar is not available.</p> <p>Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..Some text to enable scrolling..</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile orientationchange Event

• The orientationchange event is triggered when the user rotates the mobile device vertically or horizontally.

jQuery Mobile orientationchange Event

• The orientationchange event is triggered when the user rotates the mobile device vertically or horizontally.

$(window).on("orientationchange",function(){ alert("The orientation has changed!"); });

jQuery Mobile orientationchange Event

• The callback function can have one argument, the event object, which returns the orientation of the mobile device: "portrait" (the device is held in a vertical position) or "landscape" (the device is held in a horizontal position):

jQuery Mobile orientationchange Event

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecreate",function(event){ $(window).on("orientationchange",function(event){ alert("Orientation changed to: " + event.orientation); }); }); </script> </head> <body> <div data-role="page"> <div data-role="header"> <h1>The orientationchange Event</h1> </div> <div data-role="main" class="ui-content"> <p>Try to rotate your device!</p> <p><b>Note:</b> You must use a mobile device, or a mobile emulator to see the effect of this event.</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile orientationchange Event

• Because the orientationchange event is bound to the window object, we can use the window.orientation property to, for example, set different styles to distinguish between portrait and landscape views:

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

<script>

$(document).on("pagecreate",function(event){

$(window).on("orientationchange",function(){

if(window.orientation == 0)

{

$("p").text("The orientation has changed to portrait!").css({"background-color":"yellow","font-size":"300%"});

}

else

{

$("p").text("The orientation has changed to landscape!").css({"background-color":"pink","font-size":"200%"});

}

});

});

</script>

</head>

<body>

<div data-role="page">

<div data-role="header">

<h1>The orientationchange Event</h1>

</div>

<div data-role="main" class="ui-content">

<p>Try to rotate your device!</p>

<p><b>Note:</b> You must use a mobile device, or a mobile emulator to see the effect of this event.</p>

</div>

<div data-role="footer">

<h1>Footer Text</h1>

</div>

</div>

</body>

</html>

jQuery Mobile Page Events

• Events for handling pages in jQuery Mobile are divided into four categories:

– Page Initialization - Before page creation, and when the page has been created

– Page Load/Unload - When an external page is loading, unloading or encounters a failure

– Page Transition - Before and after page transitions

– Page Change - When pages are changed to or from, or encounters a failure

jQuery Mobile Initialization Events

• When a typical page in jQuery Mobile is initialized, it goes through two stages:

– Before page creation

– Page creation

jQuery Mobile Initialization Events

• Each stage has an event that can be used to insert or manipulate code before or when jQuery Mobile enhances the page.

Event Description

pagebeforecreate Triggered when the page is about to be initialized, and before jQuery Mobile has started enhancing the page

pagecreate Triggered when the page has been created, but before enhancement is complete

jQuery Mobile Initialization Events <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagebeforecreate",function(){ alert("pagebeforecreate event fired - the page is about to be initialized. jQuery Mobile has not begun enhancing the page"); }); $(document).on("pagecreate",function(){ alert("pagecreate event fired - the page has been created, but enhancement is not complete"); }); </script> </head> <body> <div data-role="page"> <div data-role="header"> <h1>Header Text</h1> </div> <div data-role="main" class="ui-content"> <p>The page has been created and enhancement is done!</p> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Load Events

• Page load events are for external pages.

• Whenever an external page is loaded into the DOM, 2 events fire.

• The first is pagecontainerbeforeload, and the second will either be pagecontainerload (success) or pagecontainerloadfailed (fail).

jQuery Mobile Load Events

Event Description

pagecontainerbeforeload

Triggered before any page load request is made

pagecontainerload Triggered after the page has been successfully loaded and inserted into the DOM

pagecontainerloadfailed

Triggered if the page load request fails. By default, it will show the "Error Loading Page" message

jQuery Mobile Load Events <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagecontainerload",function(event,data){ alert("pagecontainerload event fired!\nURL: " + data.url); }); $(document).on("pagecontainerloadfailed",function(event,data){ alert("Sorry, requested page does not exist."); }); </script> </head> <body> <div data-role="page"> <div data-role="header"> <h1>Header Text</h1> </div> <div data-role="main" class="ui-content"> <a href="externalpage.html">External page</a> <br><br> <a href="externalnotexist.html">External page that does not exist</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

jQuery Mobile Transition Events

• We can also use events for when we transition from one page to the next.

• Page transitions involve two pages: a "from" page and a "to" page - these transitions animate the change from the current active page (fromPage) to a new page (toPage).

jQuery Mobile Transition Events

Event Description

pagebeforeshow Triggered on the "to" page, before the transition animation starts

pageshow Triggered on the "to" page, after the transition animation completes

pagebeforehide Triggered on the "from" page, before the transition animation starts

pagehide Triggered on the "from" page, after the transition animation completes

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css"> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script> <script> $(document).on("pagebeforeshow","#pagetwo",function(){ alert("pagebeforeshow event fired - pagetwo is about to be shown"); }); $(document).on("pageshow","#pagetwo",function(){ alert("pageshow event fired - pagetwo is now shown"); }); $(document).on("pagebeforehide","#pagetwo",function(){ alert("pagebeforehide event fired - pagetwo is about to be hidden"); }); $(document).on("pagehide","#pagetwo",function(){ alert("pagehide event fired - pagetwo is now hidden"); }); </script> </head> <body> <div data-role="page" id="pageone"> <div data-role="header"> <h1>Header Text</h1> </div> <div data-role="main" class="ui-content"> <p>Page One</p> <a href="#pagetwo">Go to Page Two</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> <div data-role="page" id="pagetwo"> <div data-role="header"> <h1>Header Text</h1> </div> <div data-role="main" class="ui-content"> <p>Page Two</p> <a href="#pageone">Go Back to Page One</a> </div> <div data-role="footer"> <h1>Footer Text</h1> </div> </div> </body> </html>

Ajax & Interaction with server (REST & SOAP)

Ajax & Interaction with server (REST & SOAP)

• As jQuery becomes ever more widespread, we get asked a lot about integration with databases.

• This article is target at those taking their first step into the world of jQuery and integrating pages with a Database.

• The same principles could also be used with a different datasource such as XML.

Ajax & Interaction with server (REST & SOAP)

• The first method we will look at is where basic information is sent back to the client and displayed within a container.

• We start by setting the standard Javascript blocks on the page.

Ajax & Interaction with server (REST & SOAP)

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

<script type="text/javascript">

//jQuery code will go here...

</script>

Ajax & Interaction with server (REST & SOAP)

• Next we set up the html that will be located in the html of the page itself

Ajax & Interaction with server (REST & SOAP)

<ul> <li>

<label>Member ID</label>

<input id="member_id" type="text" />

<input id="blnLoadMember" type="button" value="Get Details" />

</li>

</ul>

<div id="MemberDetails"></div>

Ajax & Interaction with server (REST & SOAP)

• Next we add the jQuery script elements.

• As this is a process that will be called by some user interaction, we will need to add the script and also add an event to the button that we added to the page.

Ajax & Interaction with server (REST & SOAP)

function GetMember() {

$('input[type=button]').attr('disabled', true);

$("#MemberDetails").html('');

$("#MemberDetails").addClass("loading");

$.ajax({

type: "POST",

url: "Members.asmx/GetMemberDetails",

data: "{'MemberNumber': '" + $("#txt_id").val() + "'}",

contentType: "application/json; charset=utf-8",

dataType: "json",

success: OnGetMemberSuccess,

error: OnGetMemberError

});

}

function OnGetMemberSuccess(data, status) {

//jQuery code will go here...

}

function OnGetMemberError(request, status, error) {

//jQuery code will go here...

}

GetMember

• This is the main stage in our process and is called by the button being clicked. Once called it follows a number of stages:

– Sets the button to disabled - to prevent duplicate clicks

– Clears the html in the MemberDetails div.

– Adds the loading class to the div - this in reality would show a loading icon.

– Calls the ajax element to process the data

– Follows either the success or error results by calling the appropriate function.

GetMember

• type - you would normally use Get or Post here depending on your requirement

• url - the path to the web service asmx file and the web method you are going to call

• data - in this instance the MemberNumber that we are going to pass

• contentType, dataType - specifying we are going to use json formatting

• success - what to do on success

• error - what to do following an error

Displaying Collections

• Sometimes, we require more than to display simple text and may wish to return the data in a format that can be displayed on multiple end clients.

• In such cases, just the raw data is returned and formatted as determined by the client.

• Firstly, we enter the core elements of the page again as before

Displaying Collections

<script src="Scripts/jquery-1.4.1.js" type="text/javascript">

</script>

<script type="text/javascript">

//jQuery code will go here...

</script>

Displaying Collections

• Next we set up the html that will be located in the html of the page itself

<p>

<input id="btnLoadAll" type="button" value="Get All Members" />

</p>

<div id="MemberList"></div>

Displaying Collections

function GetAllMembers() {

$("#MemberList").html('');

$("#MemberList").addClass("loading");

$.ajax({

type: "POST",

url: "Members.asmx/GetAllMembers",

data: "{}",

contentType: "application/json; charset=utf-8",

dataType: "json",

success: OnGetAllMembersSuccess,

error: OnGetAllMembersError

});

}

function OnGetAllMembersSuccess(data, status) {

//jQuery code will go here...

}

function OnGetAllMembersError(request, status, error) {

//jQuery code will go here...

}

Displaying Collections

• Next we set up the html that will be located in the html of the page itself

<p>

<input id="btnLoadAll" type="button" value="Get All Members" />

</p>

<div id="MemberList"></div>