
datepicker,
autocompletion,
sortable,
draggable/droppable,
progressbar,
and accordion.
The tutorial describes the general methodology for configuring
jquery widgets and also walks through a number of
simple working example page configurations. JQuery comes with WHIFF
The JQueryUIBase middleware
Targetting a named widget
Configuring Options
Configuring Methods
Configuring Events
The widget types
Date Picker
Autocompletion
Accordion
Progressbar
Slider
Draggable and droppable
Sortable
Styling options
Conclusion
If you want to use a jquery feature on a web page
you must include a reference to the basic jquery library.
The whiff_middleware/jquery/jQueryLib middleware will include
the library automatically.
For example the following simple WHIFF configuration template uses the offset method
from the jquery library to find the position of a div
element on a page.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title> jquery position test </title>
</head>
<body>
{{include "whiff_middleware/jquery/jQueryLib"/}}
<br><br><br>
<center>
<div id="testDiv" onclick="showOffset()" style="background-color:yellow; width:150px">
Where am I? (click here)
</div>
</center>
<script>
function showOffset() {
var testDiv = $('#testDiv');
var offset = testDiv.offset();
alert('x: ' + offset.left + ', y: ' + offset.top);
}
</script>
</body>
</html>
{{include "whiff_middleware/jquery/jQueryLib"/}}
jQueryLib the
extra add attempts will be ignored.
Similarly if you want to use the jqueryUI add on library you may
use the directive
{{include "whiff_middleware/jquery/jQueryUILib"/}}
The jqueryUI components often require standard CSS style
definitions. WHIFF includes a default stylesheet which provides the
definitions which you can add in a WHIFF configuration as follows:
<link type="text/css"
href="whiff_middleware/jquery/css/ui-lightness/jquery-ui-1.8.2.custom.css"
rel="stylesheet" />
head of the HTML document.
You may also want to "roll your own" stylesheet to get the look and feel you prefer
using the
jqueryUI themeroller.
Please click the text with the yellow background in the embedded frame below to see the page generated by the template above in action.
jquery directly in a simple manner.
Other uses of jquery can be complex and tricky.
The JQueryUIBase middleware makes it
a bit easier to configure a jqueryUI
widget by automatically generating a number of commonly
useful javascript fragments often used with widgets.
JQueryUIBase javascript generator helpful,
but other programmers may prefer to program directly to
the jQuery UI javascript API interface.
The interfaces to jqueryUI widgets
are characterized by a target, a widget name, options, events,
and methods:
datepicker allows the user to pick a date from a "calendar"
pop-up.
JQueryUIBase middleware includes features
for configuring these widget interfaces as described below.
datepicker with the HTML input element with the
identity mydateinput
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mydateinput {{/using}}
{{using widget}} datepicker {{/using}}
{{/include}}
When is your next birthday? <input id="mydateinput" name="thedate">
datepicker section below to see
this configuration in a complete example.
JQueryUIBase middleware to
set an initial value for a widget option and to assign
javascript names for getters and setters for widget options.
For example the following slider configuration
configures a number of options for the slider widget
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myslider {{/using}}
{{using widget}} slider {{/using}}
{{using option_max}} 44 {{/using}}
{{using option_min}} -100 {{/using}}
{{using set_value}} setslider {{/using}}
{{using get_value}} getslider {{/using}}
{{using event_change}} displayslider {{/using}}
{{using event_slide}} displayslider {{/using}}
{{/include}}
In the above configuration the using directive
{{using option_max}} 44 {{/using}}
max value for the slider to the javascript integer 44.
In general an initial option value may be set to any javascript value.
The using directives
{{using set_value}} setslider {{/using}}
{{using get_value}} getslider {{/using}}
setslider and getslider
to get and set the value option of the slider widget respectively.
The getter and setter names may be used in javascript programs on the page to
change the value option during the lifetime of the slider
widget. The function call
setslider(fvalue);
value option and the function call
var slider_value = getslider();
JQueryUIBase middleware to
assign a global javascript name to the method associated with a widget.
For example the following configuration for a sortable widget
assigns the name sortOrder to the toArray widget method.
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mysortable {{/using}}
{{using widget}} sortable {{/using}}
{{using method_toArray}} sortOrder {{/using}}
{{using event_change}} showSort {{/using}}
{{using event_update}} showSort {{/using}}
{{/include}}
sortOrder method
to obtain the array from the sortable as follows:
var sortArray = sortOrder();
sortable section below for a complete example
which uses this configuration.
JQueryUIBase middleware to assign a
javascript function value to an event for a widget.
For example the sortable configuration shown above
also assigns the change and update to have
the same function value showSort. The function showSort
is defined using javascript elsewhere on the page.
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mysortable {{/using}}
{{using widget}} sortable {{/using}}
{{using method_toArray}} sortOrder {{/using}}
{{using event_change}} showSort {{/using}}
{{using event_update}} showSort {{/using}}
{{/include}}
<script>
function showSort() {
var sortArray = sortOrder();
var display = document.getElementById("display");
var report = sortArray.join(" .. ");
display.innerHTML = report;
}
</script>
jqueryUI widgets.
This document will not duplicate descriptions for the options, methods, events or styling available for configuring the widget types. Please see the jQueryUI demos and documentation site for detailed information on the widget configurations.
The source code for these examples is distributed with the WHIFF distribution
in the directory INSTALL/doc/docroot/jquery.
To browse the demos (including some demos not explained here)
please go to the
INSTALL/doc/docroot/jquery/index.html index page.
datepicker configuration allows the user to specify
the birthdate by clicking on an interactive calendar.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery datepicker WHIFF test</title>
<link type="text/css"
href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css"
rel="stylesheet" />
</head>
<body>
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mydateinput {{/using}}
{{using widget}} datepicker {{/using}}
{{/include}}
When is your next birthday? <br>
<input id="mydateinput" name="thedate">
</body>
</html>
jquery stylesheet distributed with
WHIFF in addition to using jQueryUIBase to connect a datepicker
widget to the mydateinput input element.
Please click in the input box in the embedded frame below to see this widget in action
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery autocomplete WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/ui-lightness/jquery-ui-1.8.2.custom.css" rel="stylesheet" />
</head>
<body>
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myInput {{/using}}
{{using widget}} autocomplete {{/using}}
{{using option_source}}
["c++", "java", "php", "coldfusion", "javascript", "asp",
"ruby", "python", "c", "scala", "groovy", "haskell", "perl"]
{{/using}}
{{/include}}
Pick a programming language <br>
<input id="myInput" name="thedate">
</body>
</html>
j (for example) to see the autocompletion widget in action.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery accordion WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
</head>
<body style="width:500px; font-size:8px">
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myaccordion {{/using}}
{{using widget}} accordion {{/using}}
{{/include}}
<h1>WHIFF/jquery Accordion example</h1>
<div id="myaccordion">
<h3><a href="#">Section 1</a></h3>
<div>
<p>
Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer...
</p>
</div>
<h3><a href="#">Section 2</a></h3>
<div>
<p>
Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet...
</div>
<h3><a href="#">Section 3</a></h3>
...
</div>
</body>
</html>
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery progress bar WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
</head>
<body style="width:500px">
Progress bar: <br>
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myprogressbar {{/using}}
{{using widget}} progressbar {{/using}}
{{using option_value}}79{{/using}}
{{using get_value}}get_progress{{/using}}
{{using set_value}}set_progress{{/using}}
{{/include}}
<center>
<div style="width:200px" id="myprogressbar"></div>
</center>
progress is <div id="progress_display">not known</div>
<br>
<button onclick="showProgress()">show progress number</button>
<br>
new progress: <input id="progress_input">
<br>
<button onclick="changeProgress()">change progress</button>
<script>
function showProgress() {
var progress_display = document.getElementById("progress_display");
progress_display.innerHTML = get_progress() + " percent.";
}
function changeProgress() {
var progress_input = document.getElementById("progress_input");
var progress = parseFloat(progress_input.value);
set_progress(progress);
}
</script>
</body>
</html>
value option for the progress bar
is initially set to 79 and the javascript names get_progress
and set_progress name functions to get and set the progress bar
value option. The other javascript functions on the page use the getter and setter
to interact with the progress bar.
Please try to change the progress bar using the input element and the buttons in the embedded frame below.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery progress bar WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myslider {{/using}}
{{using widget}} slider {{/using}}
{{using option_max}} 44 {{/using}}
{{using option_min}} -100 {{/using}}
{{using set_value}} setslider {{/using}}
{{using get_value}} getslider {{/using}}
{{using event_change}} displayslider {{/using}}
{{using event_slide}} displayslider {{/using}}
{{/include}}
</head>
<body style="width:500px">
<h3>WHIFF/slider demo</h3>
<blockquote>
<div id="myslider" style="width:200px"> </div>
</blockquote>
<hr>
value is <div id="display"> zero </div>
<hr>
set value to <input id="inputElement" value="0"> <button onclick="forceSlider()">SET</button>
<script>
var count = 0;
function displayslider() {
count += 1;
var value = getslider();
//al("displayslider got value "+value);
var display = document.getElementById("display");
display.innerHTML = ""+value+", ("+count+")";
}
function forceSlider() {
var inputElement = document.getElementById("inputElement");
var value = inputElement.value;
var fvalue = parseFloat(value);
setslider(fvalue);
displayslider();
}
</script>
</body>
</html>
setslider and getslider
to get and set the value for the slider. Both the change
and the slide events are given the function value displayslider
which is defined in javascript elsewhere on the page. The displayslider and
forceSlider javascript functions use the getter and setter to interact with
the slider widget.
Please drag the slider handle in the embedded frame below to see the slider value change. You may also explicitly set the slider value using the input element.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery drag/drop WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
</head>
<body style="font-family: Arial;">
<h3>WHIFF/jquery droppable demo</h3>
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mydroppable {{/using}}
{{using widget}} droppable {{/using}}
{{using event_drop}}
function(event, ui) {
var dr = ui.draggable
stuff = [ ""+dr.attr("id")+" dropped in "+$(this).attr("id") ];
var pre = document.getElementById("pre");
pre.innerHTML = stuff.join("\n\n")
}
{{/using}}
{{/include}}
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mydraggable {{/using}}
{{using widget}} draggable {{/using}}
{{/include}}
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} myOtherdraggable {{/using}}
{{using widget}} draggable {{/using}}
{{/include}}
<div id="mydroppable" style="background-color:pink; width:100px; height:100px; float:left">
Drop here
</div>
<div style="width:10px; height:10px; float:left"></div>
<div id="mydraggable" style="background-color:yellow; width:80px; height:50px; float:left">
Drag me
</div>
<div style="width:10px; height:10px; float:left"></div>
<div id="myOtherdraggable" style="background-color:cyan; width:50px; height:80px; float:left">
Drag me too
</div>
<div style="clear:left"> </div>
<br>
<pre id="pre">
Drop event information will be reported here.
</pre>
</body>
</html>
drop
droppable event which illustrates how to determine the identities for the droppable
and draggable when a draggable is dropped in a droppable.
Please drag the 'drag me' draggable boxes in the embedded iframe below and drop them into the 'drop here' droppable box.
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>jQuery sortable WHIFF test</title>
<link type="text/css" href="whiff_middleware/jquery/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
</head>
<body>
{{include "whiff_middleware/jquery/jQueryUIBase"}}
{{using targetId}} mysortable {{/using}}
{{using widget}} sortable {{/using}}
{{using method_toArray}} sortOrder {{/using}}
{{using event_change}} showSort {{/using}}
{{using event_update}} showSort {{/using}}
{{/include}}
<h3>WHIFF/jquery sortable demo</h3>
Please drag items below.
<blockquote>
<div id="mysortable" style="width:200px">
<div style="background-color:pink" id="a"> apple </div>
<div style="background-color:limegreen" id="b"> banana </div>
<div style="background-color:magenta" id="c"> cookie</div>
<div style="background-color:yellow" id="d"> donut</div>
<div style="background-color:cyan" id="e"> egg</div>
<div style="background-color:cornsilk" id="f"> fish</div>
</div>
</blockquote>
<script>
function showSort() {
var sortArray = sortOrder();
var display = document.getElementById("display");
var report = sortArray.join(" .. ");
display.innerHTML = report;
}
</script>
<div id="display"> order changes will be reported here. </div>
</body>
</html>
sortOrder to the sortable toArray method
and assigns both the change and the update events the function value
showSort which is defined as a javascript function separately.
Please drag the colored boxes to a different order in the embedded frame below.
jQueryUI widgets use standard CSS styling names
and must use a stylesheet to define these names.
If you want to use a different stylesheet than the one that comes automatically with WHIFF
you can change the style sheet reference
<link type="text/css"
href="whiff_middleware/jquery/css/ui-lightness/jquery-ui-1.8.2.custom.css"
rel="stylesheet" />
jQuery and jQueryUI javascript libraries
provide powerful methods for presenting information and
on web pages and for adding user interactions to web pages.
This tutorial showed how WHIFF includes automatic support
for these libraries and illustrated a number of working examples that
demonstrate how to use the jQueryUI widgets in a WHIFF
configuration.
|
Subsections: ++ Ajax autocompletion using jQuery
× This tutorial describes how to build and deploy a simple application which uses an Ajax server callback to provide auto completion for an input element. |
||