Change Column Boolean Value After Clicking Checkbox In Cgridview Using Ajax

I have a column with value true or false in my database. Now I created a checkbox in CGridview which when checked the value in database should change to ‘True’. How can I implement this using Ajax? My code doesn’t work well and my database is not updating.

On my view, i put something like this:




array(

		'name'=>'is_selected',             

		'value'=>'CHtml::checkBox("cid[]",null,array("value"=>$data->id,"id"=>"cid_".$data->id))',

		'type'=>'raw',

		'htmlOptions'=>array('width'=>5),

       		),	



and some javascript on my view:




<script type="text/javascript">

	function checkCandidate(target_id){

		var keyId=$.fn.yiiGridView.getSelection(target_id);

		keyId = keyId[0];		

		

		

		$.ajax({

			url:'<?php Yii::app()->createUrl('checkSelected'); ?>',

			data:{id:keyId},

			type:'GET',

			success:function(data){

				//alert(data);

			}

		});


	}

</script>



How can I make this work? I would appreciate your help. Thank you.

Check this extension to see how he does it. By the way, I use this extension and it works well.

I tried this extension but instead of displaying the toggle button, it only prompt ‘uncheck’ word on the column.

Hi

Can you copy your rules and controller code here

have u added like following rules in your model?

array(‘status’, ‘boolean’),

No. I did not add that on my model.

This is my controller:




public function actionCheckSelected($id = null){


		$result=MyModel::model()->findByPk($id);

		$result->is_selected=true;

		$result->save();	

		


	}



ok then u have to add this rule in your model to save boolean in your database.

array(‘is_selected’, ‘boolean’),

I add it already on my model but still not works for me. I guess it has something to do with ajax. I really don’t understand how Checkbox works on CGridview and how it can change the value of boolean on my database after marking the box with check. Anyway thankyou :)

In View:


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

    'id' => 'user-grid',

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

    'filter' => $model,

    'template' => "{summary}\n{pager}<br />\n{items}\n{pager}",

    'columns' => array(

...

        array(

            'class' => 'JToggleColumn',

            'name' => 'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)

            'filter' => array('0' => 'Inactive', '1' => 'Active'), // filter

            'checkedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_online.png', // Image,text-label or Html

            'uncheckedButtonLabel' => Yii::app()->request->baseUrl . '/css/icons/status_offline.png', // Image,text-label or Html

            'checkedButtonTitle' => 'Active. Click to Deactivate', // tooltip

            'uncheckedButtonTitle' => 'Inactive. Click to Activate', // tooltip

            'labeltype' => 'image', // New Option - may be 'image','html' or 'text'

            'htmlOptions' => array('style' => 'text-align:center;width:60px;')

        ),

...

In Controller:


    public function actions()

    {

        return array(

            'toggle' => 'ext.jtogglecolumn.ToggleAction',

        );

    }


    public function accessRules()

    {

        return array(

            array('allow', // allow all users to perform 'index' and 'view' actions

                'actions' => array(..., 'toggle',...);

                'users' => array('@'),

            ),

            array('deny', // deny all users

                'users' => array('*'),

            ),

        );

    }




I followed exactly what the tutorial said but i keep on getting an error. Take a look at this error:

5565

Screenshot from 2014-05-15 08:13:04.png

For the meantime I changed the value of the column on my database to see if it will work. And I get this:

5566

Screenshot from 2014-05-15 08:29:27.png

Whenever I clicked the row, the said error shows.

Forgot:

In config.php


    'import' => array(

        'application.models.*',

        'application.components.*',

        'application.components.widgets.*',


        'application.extensions.jtogglecolumn.*',


    ),

Everything works for me.

As for your code, could it be ‘extensions.’ instead of ‘ext.’?

Yeah. I tried both ways from ext. to extensions. but I still get the same error. I also add

on my config, but nothing happens. I don’t know why it is not working on my app. :’(

Your error is indicating Yii can’t find the ToggleAction file, is it in extensions/jtogglecolumn/ dir? Depending on OS it may be case sensitive.

Yes. I extracted it on protected/extensions/jtogglecolumn/ and it keeps on saying that it cannot find the directory.

Hello. I just made it work! All i did is to change the ‘extensions.jtogglecolumn.ToggleAction’ to ‘ext.jtogglecolumn.ToggleAction’ both in my controller and config.

I want to thank you jkofsky for helping me here.

No problem. +1 would be nice :D