llvk
llvk

Reputation: 248

How to add another element to Yii radioButtonList template?

I need the radioButtonList with tooltip after each element.

I use this code:

<?php echo $form->radioButtonList($model, 'Field', CHtml::listData(FieldDataModel::model()->findAll(), 'id', 'name')); ?>

and get following result:

[x] ParamName0
[ ] ParamName1
[ ] ParamName2

How can I get such result:

[x] ParamName0 [?]-(Link, that shows description. Description is taking from FieldDataModel)
[ ] ParamName1 [?]
[ ] ParamName2 [?]

May be, templates like 'template' => '{input} {label}' can help me?

Upvotes: 1

Views: 2048

Answers (2)

ezze
ezze

Reputation: 4110

As alternative solution you can still use CActiveForm and create required links with descriptions dynamically. For example, it could look like this:

<?php
$listModels = FieldDataModel::model()->findAll();
$form = $this->beginWidget('CActiveForm');
?>
<div>
<?php
echo $form->radioButtonList($model, 'Field', CHtml::listData($listModels, 'id', 'name'));
?>
</div>
<?php
$this->endWidget();

$descriptions = array();
foreach ($listModels as $listModel)
    $descriptions['id-' . $listModel->id] = $listModel->description;
$modelClassName = get_class($model);
Yii::app()->clientScript->registerScript("customRadioButtonListInit", "
    var descriptions = " . CJavaScript::encode($descriptions) . ";
    jQuery('#" . $modelClassName . "_Field input[type=\"radio\"]').each(function() {
        var valueAttr = jQuery(this).attr('value');
        var jqLabel = jQuery(this).next('label');
        var jqTooltipLink = jQuery('<a></a>')
            .text('?')
            .attr('title', descriptions['id-' + valueAttr])
            .click(function(event) {
                event.preventDefault();
                event.stopPropagation();
                // TODO: implement your tooltip here...
                alert(jQuery(this).attr('title'));
            });
        jqLabel.after(jqTooltipLink).after(' ');
    })
", CClientScript::POS_READY);

Upvotes: 0

Michael H&#228;rtl
Michael H&#228;rtl

Reputation: 8587

That's not possible with the current implementation. You have to loop over your list data and create the buttons manually with CHtml::radioButton() or CHtml::activeRadioButton().

Upvotes: 1

Related Questions