<!-- | |
#* Licensed to the Apache Software Foundation (ASF) under one | |
or more contributor license agreements. See the NOTICE file | |
distributed with this work for additional information | |
regarding copyright ownership. The ASF licenses this file | |
to you under the Apache License, Version 2.0 (the | |
"License"); you may not use this file except in compliance | |
with the License. You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, | |
software distributed under the License is distributed on an | |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
KIND, either express or implied. See the License for the | |
specific language governing permissions and limitations | |
under the License.*# | |
--> | |
This <a target="blank" class="external" href="http://en.wikipedia.org/wiki/AJAX">AJAX</a> example pulls the customer details for the selected customer and shows the | |
results without refreshing the page. | |
Please select one of the customers: | |
<p/> | |
<table cellspacing="8" cellpadding="8"> | |
<tr> | |
<td> $customerSelect </td> | |
<td> <div id="customerDetails"/> </td> | |
</tr> | |
</table> | |
<p> </p> | |
The <a target="blank" class="external" href="http://openrico.org/">Rico</a> JavaScript | |
library is used in this example. Please also see the | |
<a target="blank" class="external" href="http://openrico.org/docs/RicoAjaxEngine.pdf">RicoAjaxEngine</a> | |
for a good tutorial. | |
<p/> | |
This HTML page contains a customer Select control | |
and a <div id='<span class="st">customerDetails</span>'/> element: | |
<pre class="codeHtml"> | |
<table cellspacing="8" cellpadding="8"> | |
<tr> | |
<td> <select name="customerSelect" onclick="<span class="red">onCustomerChange</span>(this);">...</select> </td> | |
<td> <div id="<span class="st">customerDetails</span>"/> </td> | |
</tr> | |
</table> | |
</pre> | |
<p/> | |
When you click on the select a HTTP request (e.g. <tt>GET ajax-customer.htm?customerId=4424</tt>) | |
is made to the | |
<a href="$context/source-viewer.htm?filename=WEB-INF/classes/org/apache/click/examples/page/ajax/AjaxCustomer.java">AjaxCustomer</a> | |
page to get the customers details. These details are returned as XML/HTML with a content-type of 'text/xml': | |
<pre class="codeHtml"> | |
<ajax-response> | |
<response type="element" id="<span class="st">customerDetails</span>"> | |
<i>HTML content...</i> | |
</response> | |
</ajax-response> | |
</pre> | |
The Rico ajaxEngine then sets the inner text of the page's registered | |
<div id='<span class="st">customerDetails</span>'/> element with the XML content of the | |
<span style="white-space:no-wrap;"><response id="<span class="st">customerDetails</span>"></span> element. Note these element | |
ids must match. | |
<p/> | |
The Rico ajaxEngine is initialised through the JavaScript page body onload function: | |
<pre class="codeHtml"> | |
<html> | |
... | |
</html"> | |
<span class="red">$</span>jsImports | |
<script type='text/javascript' src='<a href="../assets/js/prototype.js">prototype.js</a>'></script> | |
<script type='text/javascript' src='<a href="../assets/js/rico.js">rico.js</a>'></script> | |
<script type='text/javascript'> | |
function <span class="maroon">registerAjax</span>() { | |
ajaxEngine.registerRequest('<span class="green">getCustomerInfo</span>', '<a href="$context/source-viewer.htm?filename=ajax/ajax-customer.htm">ajax-customer.htm</a>'); | |
ajaxEngine.registerAjaxElement('<span class="st">customerDetails</span>'); | |
} | |
function <span class="red">onCustomerChange</span>(select) { | |
ajaxEngine.sendRequest('<span class="green">getCustomerInfo</span>', 'customerId=' + select.value); | |
} | |
addLoadEvent(registerAjax); | |
</script> | |
</pre> | |
The initialization code above is contained in the | |
<a href="$context/source-viewer.htm?filename=ajax/ajax-include.htm">ajax-include.htm</a> file. | |
Note when including the JavaScript prototype.js and rico.js files, ensure prototype.js is before rico.js | |
as rico is dependent upon prototype. |