Best way to toggle database value via CGridView?

I have a CGridView (BootGridView to be exact, but that should be interchangeable) showing some columns from my database table. One column is a tinyint ‘boolean’ flag that represents an active/inactive data entry.

For example; my column is named ‘Active’.

In this column, depending on the data rows status, it shows an int that is either 0 or 1.

Where do I start off, to incorporate a checkbox button that switches this on/off via Ajax, directly in the grid view? A checkbox instead of the 0/1 and having to go into a different edit view.

It feels like I have probably 20 different ways to accomplish this, but I can’t figure out a really intuitive and sane approach.

There is an example in the cookbook (newest Yii book) that creates a ‘Flag’ column. In his solution he displays Y/N values and clicking toggles the value in the database.

Hi,

I’m using the code from the cookbook chapter 7 for this - the Flag Column class works as expected. However, when I implement in the admin of my site - I’ve created an admin module for this - I’m getting errors in Firebug “NetworkError: 403 CHttpException”… any ideas as to what is wrong here?

The FlagColumn class under protected/modules/admin/components is




class FlagColumn extends CGridColumn

{

	public $name;

	public $sortable=true;

	public $callbackUrl = array('flag');

	private $_flagClass = "flag_link";


	public function init() {

		parent::init();

		$cs=Yii::app()->getClientScript();

		$gridId = $this->grid->getId();

		$script = <<<script

		jQuery(".{$this->_flagClass}").live("click", function(e){

			e.preventDefault();

			var link = this;

			$.ajax({

				dataType: "json",

				cache: false,

				url: link.href,

				success: function(data){

					$('#$gridId').yiiGridView.update('$gridId');

				}

			});

		});

SCRIPT;

		$cs->registerScript(__CLASS__.$gridId.'#flag_link', $script);

	}


	protected function renderDataCellContent($row, $data) {

		$value=CHtml::value($data,$this->name);


		$this->callbackUrl['pk'] = $data->primaryKey;

		$this->callbackUrl['name'] = urlencode($this->name);

		$this->callbackUrl['value'] = (int)empty($value);

		

		$link = CHtml::normalizeUrl($this->callbackUrl);


		echo CHtml::link(!empty($value) ? 'Y' : 'N', $link, array(

			'class' => $this->_flagClass,

		));

	}


	protected function renderHeaderCellContent()

	{

		if($this->grid->enableSorting && $this->sortable && $this->name!==null)

			echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);

		else if($this->name!==null && $this->header===null)

		{

			if($this->grid->dataProvider instanceof CActiveDataProvider)

				echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));

			else

				echo CHtml::encode($this->name);

		}

		else

			parent::renderHeaderCellContent();

	}

}



Many thanks,

Hi,

Sorted it - I had restricted access to actions other than performing CRUD operations in my controller - when I added the flag action, the code worked.