Revision #37 has been created by Interboy on Nov 3, 2013, 7:07:15 AM with the memo:
add CHtml::ajaxLink() method and examples
« previous (#36) next (#46) »
Changes
Title
unchanged
By Example: CHtml
Category
unchanged
Tutorials
Yii version
unchanged
Tags
changed
listData, chtml, link, button, ByExample, ajax, ajax link
Content
changed
[...]
Smarthead will be pulling these from the forum when he is not finding the answers on his own. Please request examples using the comments below or ask for an example in the forum. Thanks.
Avaiable methods:<br>
- [CHtml::link()](#hh0)
- [CHtml::ajaxLink()](#hh1)
- [CHtml::button()](#hh
12)
- [CHtml::textField()](#hh
23)
- [CHtml::listData()](#hh
34)
- [CHtml::dropDownList()](#hh
45)
## CHtml::link() method[...]
```
## CHtml::ajaxLink() method
Syntax:
```php
public static string ajaxLink(string $text, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
```
**Default example**
```php
echo CHtml::ajaxLink(
$text = 'Click me',
$url = '/',
$ajaxOptions=array (
'type'=>'POST',
'dataType'=>'json',
'success'=>'function(html){ jQuery("#your_id").html(html); }'
),
$htmlOptions=array ()
);
```
**Example #1 : Ajax request using ajaxLink**
```php
//In view:
echo CHtml::ajaxLink(
'Test request', // the link body (it will NOT be HTML-encoded.)
array('ajax/reqTest01'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
array(
'update'=>'#req_res'
)
);
echo '<div id="req_res">...</div>';
//In controller
public function actionReqTest01() {
echo date('H:i:s');
Yii::app()->end();
}
```
**Example #2 : Ajax request using ajaxLink with loading image**
```php
//In view:
echo CHtml::ajaxLink(
'Test request', // the link body (it will NOT be HTML-encoded.)
array('ajax/reqTest01Loading'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
array(
'update'=>'#req_res_loading',
'beforeSend' => 'function() {
$("#maindiv").addClass("loading");
}',
'complete' => 'function() {
$("#maindiv").removeClass("loading");
}',
)
);
echo '<div id="req_res_loading">...</div>';
//In controller:
public function actionReqTest01Loading() {
sleep(4); // Sleep for 4 seconds just to demonstrate the Loading Image can be seen, for learning purpose only
echo date('H:i:s');
Yii::app()->end();
}
```
Reference: [Ajaxlink](http://www.codexamples.com/89/chtml-ajaxlink/ "")
## CHtml::button() method
~~~[...]
***
You can find CHtml class at `yii/framework/web/helpers/CHtml.php` view on [Github](https://github.com/yiisoft/yii/blob/1.1.14/framework/web/helpers/CHtml.php ""), all [CHtml methods here](http://yii.codexamples.com/v1.1/CHtml/ "").
## Links[...]
### Japanese
- [CHtmlの使い方](http://yiijan.org/tutorials/view/chtml%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9)