Zugluk
(Lacffy Duck)
November 5, 2012, 10:19am
1
Hello,
I would like to add a prefix before all my dropdownlist element. For example, I generate a dropdownlist which gives me all addresses of all my servers written in a table, however I would like for visibility add "http://" before each address in my drop down list.
Have you an idea ?
thanks
Bianca
(Biancajsen)
November 5, 2012, 12:39pm
2
Zugluk:
Hello,
I would like to add a prefix before all my dropdownlist element. For example, I generate a dropdownlist which gives me all addresses of all my servers written in a table, however I would like for visibility add "http://" before each address in my drop down list.
Have you an idea ?
thanks
You can add a method in your model like this:-
public function getXXX()
{
return 'http://'.$this->server_address;
}
And then add in your view for your dropdownlist, for example:-
<?php echo $form->dropDownList($model,'yourtable',
CHtml::listData(Yourtable::model()->findAll(), 'yourtable_id', 'XXX'),array('prompt' => 'Select')); ?>
xahgmah
(Xahgmah)
November 5, 2012, 12:44pm
3
You can use own method extend default CHtml::listData($model,"key","value");
public static function listDataWithPrefix($models,$valueField,$textField,$groupField='',$prefix = "")
{
$listData=array();
if($groupField==='')
{
foreach($models as $model)
{
$value=self::value($model,$valueField);
$text=$prefix . self::value($model,$textField);
$listData[$value]=$text;
}
}
else
{
foreach($models as $model)
{
$group=self::value($model,$groupField);
$value=self::value($model,$valueField);
$text=$prefix . self::value($model,$textField);
$listData[$group][$value]=$text;
}
}
return $listData;
}
Zugluk
(Lacffy Duck)
November 5, 2012, 12:48pm
4
hmmm ok, thanks you both, I thought there would be an html option to put in chtml::dropdownlist to do it, but apparently not.