Global place to define CGridView Buttons

Hi all,

I have a CGridView with following button -




         ....

         'buttons' => array(

                'view' => array(

                    'label' => 'view',

                    'url'   => 'Yii::app()->controller->createUrl("view",array("id"=>$data->id))',

                    'options' => array(

                        'ajax' => array(

                            'type' => 'POST',

                            'url' => 'js:$(this).attr("href")',

                            'success' => 'js:function(data){$("#view-page").html(data);}',

                        ),

                    ),

                ),

                'update' => array(

                    'label' => 'update',

                    'url'   => 'Yii::app()->controller->createUrl("update",array("id"=>$data->id))',

                    'options' => array(

                        'ajax' => array(

                            'type' => 'POST',

                            'url' => 'js:$(this).attr("href")',

                            'success' => 'js:function(data){$("#view-page").html(data);}',

                        ),

                    ),

                ),

                'delete' => array(

                    'url'   => 'Yii::app()->controller->createUrl("delete",array("id"=>$data->id))',

                    'label' => 'delete',

                    'options' => array(

                        'ajax' => array(

                            'type' => 'POST',

                            'url' => 'js:$(this).attr("href")',

                            'success' => 'js:function(data){$("#view-page").html(data);}',

                        ),

                    ),

               ),

             ''''

This works fine. But I have to implement the same buttons in all the gridviews and I do not want to copy all the above code everywhere in all my views containing gridview.

Is there any global place where I can specify this?

I tried components/widgetFactory setting in conf/main.php without any luck.

Please help!

Thanks.

You should create your own Grid View class in /protected/components.

e.g. (you may also use this class)




<?php


Yii::import("zii.widgets.grid.CGridView");


/**

 * Description of VGridView

 *

 * @author vinay

 */

class VGridView extends CGridView {


	protected function initColumns() {

    	$this->columns['buttons'] = array(

        	'view' => array(

            	'label' => 'view',

            	'url' => 'Yii::app()->controller->createUrl("view",array("id"=>$data->id))',

            	'options' => array(

                	'ajax' => array(

                    	'type' => 'POST',

                    	'url' => 'js:$(this).attr("href")',

                    	'success' => 'js:function(data){$("#view-page").html(data);}',

                	),

            	),

        	),

        	'update' => array(

            	'label' => 'update',

            	'url' => 'Yii::app()->controller->createUrl("update",array("id"=>$data->id))',

            	'options' => array(

                	'ajax' => array(

                    	'type' => 'POST',

                    	'url' => 'js:$(this).attr("href")',

                    	'success' => 'js:function(data){$("#view-page").html(data);}',

                	),

            	),

        	),

        	'delete' => array(

            	'url' => 'Yii::app()->controller->createUrl("delete",array("id"=>$data->id))',

            	'label' => 'delete',

            	'options' => array(

                	'ajax' => array(

                    	'type' => 'POST',

                    	'url' => 'js:$(this).attr("href")',

                    	'success' => 'js:function(data){$("#view-page").html(data);}',

                	),

            	),

        	),

    	);

    	parent::initColumns();

	}


}




And use VGridView instead of CGridView as




$this->widget('VGridView', array(

	'col1',

	'col2',

   ...

));



Thanks for the reply Vinay.

I will try this!

Thanks,

Sachin.

Hi Vinay,

I am getting exception as -

Property "CDataColumn.view" is not defined.

Thanks,

Sachin.

Extended CButtonColumn class -




<?php

/**

 * Description of AjaxButtonColumn

 *

 */

class AjaxButtonColumn extends CButtonColumn {


    protected function initDefaultButtons() {

        $this->buttons = array(

            'view' => array(

                'label' => 'view',

                'url' => 'Yii::app()->controller->createUrl("view",array("id"=>$data->id))',

                'options' => array(

                    'ajax' => array(

                        'type' => 'POST',

                        'url' => 'js:$(this).attr("href")',

                        'success' => 'js:function(data){$("#view-page").html(data);}',

                    ),

                ),

            ),

        'update' => array(

            'label' => 'update',

            'url' => 'Yii::app()->controller->createUrl("update",array("id"=>$data->id))',

            'options' => array(

                'ajax' => array(

                    'type' => 'POST',

                    'url' => 'js:$(this).attr("href")',

                    'success' => 'js:function(data){$("#view-page").html(data);}',

                ),

            ),

        ),

        'delete' => array(

            'url' => 'Yii::app()->controller->createUrl("delete",array("id"=>$data->id))',

            'label' => 'delete',

            'options' => array(

                'ajax' => array(

                    'type' => 'POST',

                    'url' => 'js:$(this).attr("href")',

                    'success' => 'js:function(data){$("#view-page").html(data);}',

                ),

            ),

        ),

    );

    parent::initDefaultButtons();

    }

}

?>



And Changed my admin view grid -


<?php $this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'view-id',

    'dataProvider'=>$model->search(),

    'ajaxUpdate'=>true,

    'columns'=>array(

        'column1',

        'column2',

        ...

        array(

                'class'=>'AjaxButtonColumn',

            ),

        ),

));




The above two changes served the purpose!

Thanks All,

Sachin.