Table of contents
Table of Contents | ||
---|---|---|
|
Using jQuery and Ajax
About jQuery
jQuery is a solid JavaScript frameworklibrary. Among its many advantages:
...
You can learn more about jQuery on the official website: http://jquery.com/
jQuery's $
jQuery has the $()
function, and the $
namespace, which contains all the other functions.
...
- When using a function as its parameter,
$()
will execute the function once the page's DOM is fully loaded. For instance:$(function(){ /* do something */ });
- When passing a string containing a CSS selector,
$()
will return all the HTML nodes which match the selector. For instance:$('ul#nav');
That set of nodes can then be assigned to any of jQuery's methods. For instance, if you
wantedwant to hide the navigation element returned by the above selector:
$('ul#nav').slideUp('fast');
When using pure HTML code as its parameter,
$()
will create the node (DOM element). That node can, again, be used with any of jQuery's methods:$("<li>Sign Off</li>").appendTo("ul#nav");
Note |
---|
If you are mixing JavaScript frameworks libraries in your theme, you might be better off by telling jQuery to free |
You can learn more about jQuery()
and $()
on the official website: http://api.jquery.com/jQuery/
Functions in jQuery
jQuery's $
namespace contains all the functions that are not attached to a specific node.
...
You can attach events to a set of nodes returned by $()
. One of the advantages of using jQuery, once again, is that when handling events, it helps you by harmonizing so that you do not have to cater for each browser specifics.
In order to association associate a function to a click event, simple add .click(function)
.
In order to generate a click event, you can also use .click()
with any parameter. For instance: $('#button').click(function(){/* do something */});
You can learn more about jQuery's Ajax and utilitarian function functions on the official website:
- http://api.jquery.com/category/ajax/ (for instance, http://api.jquery.com/jQuery.post/)
- http://api.jquery.com/category/utilities/
Selectors
jQuery offers two ways to selection select page elements:
- Combine CSS and XPath selectors in a string, which is used with the
$()
function. For instance:$("div > ul a")
- Using one of the several methods already available in the
jQuery
namespace.
...
You can learn more about jQuery's selectors on the official website: http://api.jquery.com/category/selectors/
Methods and callbacks
A whole set of methods are available in the standard API: DOM manipulation, CSS manipulation, event management, visual effects, etc.
...
Code Block |
---|
$(".emptyContent").append("This is a test") .css("border", "1px solid red") .addClass("fullContent") .removeClass("emptyContent"); |
Available plugins in PrestaShop
Here is a list of the jQuery plugins that are available in a default installation of PrestaShop:
Plugin file name | Plugin description |
---|---|
jquery.colorpicker | Photoshop-style color selector. |
jquery.cookie-plugin | Read, write and delete cookies. |
jquery.dimensions | Manage an element's dimensions. |
jquery.easing | Manage the speed of an animation. |
jquery.excanvas | Change the canvas of an element (rounded corners / gradients / opacity / draw line, arc, etc.) |
jquery.fieldselection | Use and replace the text selected within a zone. |
jquery.flip | Flip an element. |
jquery.flot | Create graphs presenting data as curves, bars, etc. |
jquery.highlight | Add syntax colorization. |
jquery.hoverIntent | Add a prediction effect on JavaScript's hover event. |
jquery.idTabs | Manage tabs. |
jquery.ifxtransfer | Animate an element with a transfer from one container to another. |
jquery.jqminmax | Add min-width, max-width, min-height and max-height on all browsers. |
jquery.pngFix | Manage transparency in IE 5.5 and IE 6. |
jquery.scrollTo | Make the page or element scroll to a certain position. |
jquery.serialScroll | Make a series of element scroll to a certain position. |
jquery.tablednd | Drag and drop a table's rows. |
jquery.typewatch | Execute a function when the user has typed some text in a zone and has stopped typing after a certain amount of time. |
jquery.validate-creditcard | Validate a credit card number depending on its type. |
A couple other functions
jQuery's API is incredibly complete, and you will spend hours finding new possibilities.
...
Code Block |
---|
if($.browser.msie) { if($.browser.version == 6) { // Your IE6 specific code. } else { // Code for any other IE browser. } } |
Making Ajax calls with jQuery
About Ajax
The term "Ajax" is really an acronym for Asynchronous JavaScript and XML. It describes the use of JavaScript to load new content into the current page without having to reload the whole page. The process is called asynchronous because once the Ajax request has been sent to the web server, the browser does not have to wait for the answer in order to perform other tasks. The transferred content does not have to be formatted in XML: it can use JSON, HTML or plain text.
In effect, using Ajax makes it possible to build very dynamic websites
You can learn about the Ajax technique on the following sites:
- Wikipedia: http://en.wikipedia.org/wiki/Ajax_%28programming%29
- Mozilla Developer Network: https://developer.mozilla.org/en/docs/AJAX
Note | ||
---|---|---|
| ||
JavaScript Object Notation is the most used format when transferring data using the Ajax technique, for two main reasons: it is considered lighter than XML, and it can very easily be used by JavaScript, as it resembles a subset of that language. You can learn more about JSON on the following sites : |
How to use Ajax in PrestaShop
By default, PrestaShop's controllers use the standard full-page-reload process.
Once PrestaShop detects the "ajax" parameter in a GET or POST query, the controller's ajax
property is switched to true: $this->ajax = true;
.
For instance, if your code triggers a URL like such: http://...&ajax&action=updatelist
...the controller will then execute the displayAjaxUpdateList()
method if it exists. If it doesn't exist, it will execute the displayAjax()
method (by default).
You therefore have to include the code that will take the Ajax call into account. Here is how you would write a simple Ajax query using jQuery:
Code Block |
---|
var query = $.ajax({
type: 'POST',
url: baseDir + 'modules/mymodule/ajax.php',
data: 'method=myMethod&id_data=' + $('#id_data').val(),
dataType: 'json',
success: function(json) {
// ....
}
}); |
And here is how the ajax.php
script would work:
Code Block |
---|
// Located in /modules/mymodule/ajax.php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
switch (Tools::getValue('method')) {
case 'myMethod' :
die( Tools::jsonEncode( array('result'=>'my_value'));
break;
default:
exit;
}
exit; |
As you can see in the code sample above, PrestaShop's Tools
object contains jsonEncode()
, a method that makes it easy to turn a PHP array into a JSON object:
Code Block |
---|
public function displayAjax() {
$return = array(
'hasError' => true,
'errors' => 'Ceci est le message'
);
die(Tools::jsonEncode($return));
} |
jQuery's Ajax methods
jQuery.ajax(url [, settings ] )
Parameters:
- url: the URL to which the query should be sent
- settings: options and functions
Some options:
async
(boolean): default is truetype
: default is 'GET'cache
(boolean): default is truedata
: GET array sent to the serverdataType
: eitherxml
,json
,script
orhtml
Some functions:
beforeSend
: triggered before the Ajax callerror
: in case of errorsuccess
: in case of successtimeout
: in case of timeoutcomplete
: triggered after the call's success or error, whatever the result
You can learn more about ajax()
here: http://api.jquery.com/jQuery.ajax/
Alternative: jQuery.load(url [, data ] [, complete(responseText, textStatus, XMLHttpRequest) ] )
This method is used to directly load the HTML result to an Ajax call, right within an element on the current page.
For instance:
Code Block |
---|
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
}); |
Alternative: jQuery.get( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
This method enables you to call an Ajax script with an HTTP GET query. It is equivalent to the following Ajax call:
Code Block |
---|
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
}); |
Return values on a jQuery Ajax call
All these jQuery methods return a jqHXR
object, which is an extension of the XMLHTTPRequest
object. That object makes it possible to trigger various functions depending on the result of the call:
Success:
jqXHR.done(function(data, textStatus, jqXHR) {});
Error:
jqXHR.fail(function(jqXHR, textStatus, errorThrown) {});
Success or Error:
jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown){ });
Success AND Error:
jqXHR.then(function(data, textStatus, jqXHR) {}, function(jqXHR, textStatus, errorThrown) {});
Here is an example with functions calls depending on the query's result:
Code Block |
---|
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.get("example.php", function() {
alert("success");
})
.done(function() { alert("second success"); })
.fail(function() { alert("error"); })
.always(function() { alert("finished"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.always(function(){ alert("second finished"); }); |