You are viewing revision #5 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
"By Example" cookbook pages will provide coding examples for many of the commonly used classes within Yii. We will try to provide as many usage examples as possible for keep these pages as helpful as possible.
CHtml::link() method ¶
public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
Generates a hyperlink tag.
Example 1: Linking to a controller action ¶
<?php echo CHtml::link('Link Text',array('controller/action')); ?>
HTML Output: ¶
<a href="index.php?r=controller/action">Link Text</a>
Example 2: Linking to a controller action with querystring parameters ¶
<?php echo CHtml::link('Link Text',array('controller/action',
'param1'=>'value1')); ?>
HTML Output: ¶
<a href="index.php?r=controller/action¶m1=value1">Link Text</a>
Example 3: Linking to a controller action with multiple querystring parameters ¶
<?php echo CHtml::link('Link Text',array('controller/action',
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>
HTML Output: ¶
<a href="index.php?r=controller/action¶m1=value1¶m2=value2¶m3=value3">Link Text</a>
Link with confiirmation
I think we also need to add link with confirmation, especially in delete condition
CHtml::link(t("Delete"), "#", array("submit"=>array("delete","id"=>"$model->id" ),"confirm"=>"Are you sure?"));
Want more
This is a great Wiki,
Want more something like this.
Button with confirmation
CHtml::button('Delete', array('submit' => array('user/delete','id'=>$model->usId), 'name'=>'btnDelete', 'confirm'=>'Are you sure you want to delete?', 'class'=>'btn btn-large btn-danger', 'style'=>'width:160px;' ));
That's what a call clear doc
Something as clear as that, should exist for the rest.
Maybe a "Yii By Exemple" wiki categ, where we could all contribute articles like that one ?!
CHtml::link, urlManager and Ajax url problems
Hi guys
I have this line in my urlManager:
'<controller:\w+>' =>'<controller>/admin', // if no action is included, then // use actionAdmin
However, when the user clicks on the CHtml::link to go to actionAdmin, then the above line causes CHtml::link to NOT include the action's name in the url in the browser's address bar.
This seems to cause problems when Ajax has to use the url.
So, if you get Ajax 404 and 'response failed' errors, start by making sure that your url contains both controller and action names.
(I'm not an Ajax expert, so if this sounds like nonsense, please let me know.)
links with image
can display image link instead of text link ?
RE: can display image link instead of text link ?
Yes.
Example:
<?php $image = CHtml::image(Yii::app()->baseUrl.'/images/thumb-1.jpg'); echo CHtml::link($image, Yii::app()->baseUrl.'/images/image-1.jpg'); ?>
Dropdown with selected value
how do u create a dropdown with a default selected value using CHtml::dropdownlist & ListData?
onmouseover
CHtml::link(
CHtml::encode($data->name), array('view', 'id'=>$data->idCategory), array('class'=>'linkClass','onmouseover'=>'doSomething();')
);
For Question #13460 about default item on dropdownlist
Dear,
For selected item by dafault you need to use the next option: for example item id 78 must be the default.
array('options' => array('78'=>array('selected'=>true)))
Here you have how to use it:
<?php echo $form->dropDownList($model,'estado_id',CHtml::listData(estado::model()->findAll(),'id', 'estado'),array('options' => array('78'=>array('selected'=>true)))); ?>
Regards!
Text Area Readonly
example
<?php echo CHtml::textArea('Text', $content, array('id'=>'widget', 'readonly' => true, 'style'=>'width:700px;height:340px;')); ?>
Custom Data Attributes
Does anyone knows how to add Custom Data Attributes (HTML5 data-*) to dropDownList
Country table (id, name, iso_code, calling_code)
<select name="Country[id]" id="Country_id"> <option value="">Select Country</option> <option value="1" data-ISO="USA">United States (+1)</option> <option value="1" data-ISO="GB">United Kingdom (+44)</option> </select>
echo $form->dropDownList(Country::model(), 'id', CHtml::listData(Country::model()->findAll(), 'id','fullName'), array('prompt' => 'Select Country', ); function getFullName() { return $this->country_short_name.' (+'.$this->calling_code.')'; }
Error on Example 3: Generating a textfield with customized id, width and maxlength
Example 3: Generating a textfield with customized id, width and maxlength
<?php echo CHtml::textField('Text', 'some value', array('id'=>'idTextField', 'width'=>100, 'maxlength'=>100)); ?>
missing one ')'
creating id
how can we create a id for the dropdown.
creating id
$options = array( 'class' => 'singleline', 'size' => '1', 'id' => 'myId' 'empty' => '', 'style' => 'width:300px;', ); echo $form->dropDownList($model, 'columnName', $listdata, $options); or echo $form->dropDownList($name, $defaultValue, $listdata, $options);
The $name is sent to the server.
The id (part of options) is used by jquery.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.