Commits

Nikola Trifunović  committed 72c0dc0

Added readme

  • Participants
  • Parent commits 7b71aed

Comments (0)

Files changed (1)

+JToggleColumn
+=====
+Column for CGrid which toggles the boolean ( TINYINT(1) ) value of model attribute. Tested with Yii 1.10.
+
+Example
+====
+![JToggleColumn](https://bitbucket.org/johonunu/jtogglecolumn/raw/7b71aed990d7/example.png)
+
+Tutorial
+====
+Define a JToggleColumn in your CGrid widget:
+
+    <?php $this->widget('zii.widgets.grid.CGridView', array(
+        'id'=>'user-grid',
+        'dataProvider'=>$model->search(),
+        'filter'=>$model,
+        'columns'=>array(
+            'id',
+            'username',
+            'password',
+            'email',
+            'role',
+            array(
+                'class'=>'JToggleColumn',
+                'name'=>'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
+                'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
+            ),
+            array(
+                'class'=>'JToggleColumn',
+                'name'=>'visible', // boolean model attribute (tinyint(1) with values 0 or 1)
+                'checkedButtonImageUrl'=>'/auth/images/check.png', // checked image
+                'uncheckedButtonImageUrl'=>'/auth/images/uncheck.png', // unchecked image
+                'checkedButtonLabel'=>'Uncheck this', // tooltip
+                'uncheckedButtonLabel'=>'Check this', // tooltip
+                'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
+            ),
+            array(
+                'class'=>'CButtonColumn',
+            ),
+        ),
+        ));?>
+ 
+Create action in your controller:
+
+    <?php
+    
+    public function actionToggle($id,$attribute)
+    {
+        if(Yii::app()->request->isPostRequest)
+        {
+            // we only allow deletion via POST request
+            $model = $this->loadModel($id);
+            $model->$attribute = !$model->$attribute;
+            $model->save();
+            
+            // 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.');
+        }
+    
+    ?>
+
+Don't forget to add this action to controllers accessRules.