phpyiicustom-data-attributeyii-chtml

Yii radioButtonList with listData and html data attribute


I am trying to output CHtml::radioButtonList (yii 1.1.14) with each rendered element looking something like the following:

<input id="option_0" type="radio" name="MyForm[customOption_id]" value="1" data-ref="a string contained in table, column ref">
<label for="option_0">Some option</label>

It is the data-ref attribute which is causing me a headache.

Code

<li>
<?php 
echo $form->radioButtonList(
        $model,'refinement_id',
        CHtml::listData($refinements, 'id', 'label'),
        array('separator'=>'</li><li>', 'container' => '')); 
?>
</li>

I want to output a data-ref attribute for each which I can then use in some JavaScript.

As far as I can tell I cannot add this to the htmlOptions array for the radioButtonList as I can only provide a static value here.

Any pointers here or someone telling me I am barking up the wrong tree would be very much appreciated.


Solution

  • Content of data-ref depends on the value or static? if it's not static, impossible with using $form->radioButtonList, do this with old-school method. Otherwise it's simple

    <?php 
    echo $form->radioButtonList(
        $model,'refinement_id',
        CHtml::listData($refinements, 'id', 'label'),
        array('separator'=>'</li><li>', 'container' => '', 'data-ref'=>'fshafh')); 
    ?>