Osama Jetawe Osama Jetawe - 5 months ago 107
PHP Question

Yii cgridview add custom html attribute to buttons from data not working

I tried to add custom html attribute to cgridview buttons from dataProvider , Im using bootstrap yii bootstrap bootstrap.widgets.TbButtonColumn

I tried

'pbs_id'=>'{$data->pbs_id}',


and

'pbs_id'=>'{$data["psp_id"]}',


but it return it as string

$this->widget('bootstrap.widgets.TbGridView', array(
'type'=>'striped bordered condensed',
'dataProvider'=>$db_slabs_data,
'template'=>"{items}",
'columns'=>array(
array('name'=> 'pbs_id', 'header'=>'Slab Id'),
array('name'=> 'pbs_name', 'header'=>'Slab Name'),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>"{update}",
'updateButtonUrl'=>'Yii::app()->createUrl("", array("pbs_id"=>$data["pbs_id"]))',
'buttons'=>array
(
'update' => array
(
'label'=> Yii::t('common', 'LBL_UPDATE'),
'icon'=>'icon-pencil',
'url'=>'Yii::app()->createUrl("", array("pbs_id"=>$data["pbs_id"]))',
'options'=>array(
'pbs_id'=>'{$data["psp_id"]}',
),
),

),

),
),
));


ad the result is :

<a href="" pbs_id=1" rel="tooltip" title="" pbs_id="{$data-&gt;pbs_id}" data-original-title="Update"><i class="icon-pencil"></i></a>


how I can do that.

Thanks

Answer

I solved my problem by:

inside this file /protected/extensions/bootstrap/widgets/TbButtonColumn.php there are a method renderButton()

I override this method to to render additional html attribute for buttons:

protected function renderButton($id, $button, $row, $data) {
        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row' => $row, 'data' => $data)))
            return;

        $label = isset($button['label']) ? $button['label'] : $id;
        $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
        $options = isset($button['options']) ? $button['options'] : array();

        /* added to render additional html attribute */
        if (isset($button['options']) AND !(empty($button['options']))) {
            foreach ($button['options'] as $key => $value) {
                if (preg_match('#\$(data|row)#', $value)) {
                    $options["$key"] = $this->evaluateExpression($button['options'][$key], array('data' => $data, 'row' => $row));
                } else {
                    $options["$key"] = $value;
                }
            }
        }

        /* end */

        if (!isset($options['title']))
            $options['title'] = $label;

        if (!isset($options['rel']))
            $options['rel'] = 'tooltip';

        if (isset($button['icon'])) {
            if (strpos($button['icon'], 'icon') === false)
                $button['icon'] = 'icon-' . implode(' icon-', explode(' ', $button['icon']));

            echo CHtml::link('<i class="' . $button['icon'] . '"></i>', $url, $options);
        }
        else if (isset($button['imageUrl']) && is_string($button['imageUrl']))
            echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options);
        else
            echo CHtml::link($label, $url, $options);
    }

and inside the grid

'buttons'=>array
                (   
                    'update' => array
                    (
                        'label'=> Yii::t('common', 'LBL_UPDATE'),
                        'icon'=>'icon-pencil',
                        'url'=>'Yii::app()->createUrl("", array("pbs_id"=>$data["pbs_id"]))',
                        'options'=>array(
                            'id'=>'$data["id"]',
                            'new_attribute'=> '$data["your_key"]',
                        ),
                    ),

                ),