两种方式使用ajax ,JS API or 数据 API. 数据API不用写任何js代码
在 主题中添加以下标签
<script src="{{ 'assets/javascript/jquery.js'|theme }}"></script>
{% framework %}
可选标签.
{% framework extras %}
A page can issue an AJAX request either prompted by data attributes or by using JavaScript. Each request invokes an event handler -- also called an AJAX handler -- on the server and can update page elements using partials. AJAX requests work best with forms, since the form data is automatically sent to the server. Here is request workflow:
update
option.Note: Depending on the page context a CMS partial or backend partial view will be rendered.
Below is a simple example that uses the data attributes API to define an AJAX enabled form. The form will issue an AJAX request to the onTest handler and requests that the result container be updated with the mypartial partial markup.
<!-- AJAX enabled form -->
<form data-request="onTest" data-request-update="mypartial: '#myDiv'">
<!-- Input two values -->
<input name="value1"> + <input name="value2">
<!-- Action button -->
<button type="submit">Calculate</button>
</form>
<!-- Result container -->
<div id="myDiv"></div>
Note: The form data for
value1
andvalue2
are automatically sent with the AJAX request.
The mypartial partial contains markup that reads the result
variable.
The result is {{ result }}
The onTest handler method accessed the form data using the input
helper method and the result is passed to the result
page variable.
function onTest()
{
$this->page['result'] = input('value1') + input('value2');
}
The example could be read like this: "When the form is submitted, issue an AJAX request to the onTest handler. When the handler finishes, render the mypartial partial and inject its contents to the #myDiv element."