Expression For Image's Url In Cbuttoncolumn

Hi guys.

Sometimes I want some buttons in CButtonColumn::buttons to have an image URL depending on $data contents of each CGridView’s row. So it would be nice to have an alternative way to specify imageUrl as PHP expression.

Hello Ezze! At this moment the easy way would be to extend the cbutton class and put the class in protected/components, could be something like this:




class CButtonColumnImageURL extends CButtonColumn

{


    /**

     * Renders a link button.

     * @param string $id the ID of the button

     * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.

     * See {@link buttons} for more details.

     * @param integer $row the row number (zero-based)

     * @param mixed $data the data object associated with the row

     */

    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)) : '#';

        $imageUrl = isset($button['imageUrl']) ? $this->evaluateExpression($button['imageUrl'], array('data' => $data, 'row' => $row)) : '';

        $options = isset($button['options']) ? $button['options'] : array();

        if (!isset($options['title'])) {

            $options['title'] = $label;

        }

        echo CHtml::link(CHtml::image($imageUrl, $label), $url, $options);

    }


}



Then in the cgridview widget you could do something like this:




        array(

            'class' => 'CButtonColumnImageURL',

            'template' => '{customButton}',

            'buttons' => array(

                'customButton' => array(

                    'label' => '',

                    'imageUrl' => 'MyModel::getImage($data->id)',

                    'url' => 'MyModel::getURL($data->id)',

                ),

            ),

        ),