By Example: CHtml

Comparing #36 with #37

Revision #37 was created by Interboy Interboy on Nov 3, 2013, 7:07:15 AM.

add CHtml::ajaxLink() method and examples

Tags

listData, chtml, link, button, ByExample, ajax, ajax link

Content

[...]
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()](#hh12) - [CHtml::textField()](#hh23) - [CHtml::listData()](#hh34) - [CHtml::dropDownList()](#hh45)

## 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)