Hello If you are using JToggleColumn extension with GxController then the loadModel syntax look like following:

$this->loadModel($id, ‘ModelName’),

So if we want to use JToggleColumn extension following changes needed in extension so we can dynamically use toggle button.

changes in JToggleColumn.php

1. declare public variable called

public $model_name = null;

2. put following code in init() function for exception

 if ($this->model_name === null)
            throw new CException(Yii::t(‘toggle_column’, ‘Model attribute (“model_name”) must be specified for CToggleColumn.’));
 3. put following code in initDefaultButtons() function

$this->toggle_button = array(
                ‘url’ => ‘Yii::app()->controller->createUrl(“‘ . $this->action . ‘”,array(“id”=>$data->primaryKey,”attribute”=>”‘ . $this->name . ‘”,”model_name”=>”‘.$this->model_name.'”))’,
                ‘options’ => array(‘class’ => $this->name . ‘_toggle’),
            );

changes in ToggleAction.php

public function run($id,$attribute,$model_name) {
        
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $model = $this->controller->loadModel($id,$model_name);
            $model->$attribute = ($model->$attribute==0)?1:0;
            $model->save(false);
            
            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array(‘admin’));
        }
        else
        throw new CHttpException(400,’Invalid request. Please do not repeat this request again.’);
    }

This is Done..

If you want to put togglebutton in gridview this the column code look like following…

array(
                    ‘class’=>’JToggleColumn’,
                    ‘name’=>’active’, // boolean model attribute (tinyint(1) with values 0 or 1)
                    ‘model_name’=>’ModelName’,             //here is my model name so click on button change done in table related this model
                    ‘filter’ => array(‘1′ => ‘Yes’,’0′ => ‘No’), // filter
            ‘action’=>’toggle’, // other action, default is ‘toggle’ action
            ‘checkedButtonLabel’=>Yii::app()->baseUrl.’/images/checked.png’, // tooltip
                    ‘uncheckedButtonLabel’=>Yii::app()->baseUrl.’/images/unchecked.png’,
                ‘labeltype’=>’image’,
                    ‘htmlOptions’=>array(‘style’=>’text-align:center;min-width:60px;’)
                ),

 

So by this changes you can dynamically toggle the column value..

Thankx…

About these ads