I would like to have a CGridView column which will toggle a boolean value when clicked upon.
e.g. I have a field called ‘enabled’ in my database table, but instead of the user having to go in to each record to update the value to yes/no it would be a lot easier if they could do this on the grid.
Just for the record, I created this example which does exactly what you want to do, based on a CButtonColumn (a button labeled Publish/Unpublish in this case).
My guess is that you probably can accomplish this with less effort, using a CCheckBoxColumn.
Edit:
Or perhaps add checkbox conrols to a CDataColumn (I can’t remember the exact reason why I, some time ago, considered that solution).
It will be rendered as an ajaxLink so I think that will fit your need. What makes it a bit complicated is the two different ways to set the label (initially vs feedback from controller action). And as mentioned, I should have chose a different name than ‘ajax’ for the array key I added.
public function actionAjaxUpdate()
{
if(isset($_GET['id']))
{
$model=$this->loadModel();
$model->enabled = ($model->enabled == 1 ? 0 : 1);
$model->update();
}
}
The link is rendered and works fine, only problem is it’s not a “proper” ajax call, i.e. the URL is reloaded and the grid disappears after the link is clicked. The field is however been updated in the database. What have I missed out?
BTW, I just return an record id from the controller in my example. Not used for updating the grid (I think it makes it’s own ajax call, have to check the framework scripts to recall).
OK finally got that to parse properly - again the database is updated but the link text does not update.
Had a look in Firebug console and there is nothing in the "Response" tab - do I need to return a JSON with the value "Yes" or "No" ? Surely that could be simplified somehow, i.e. if the link gets clicked then it just needs to toggle the values (providing the update was successful).
I’m out of suggestions on this (obviously you use a CDataColumn). BTW, I forgot to mention CLinkColumn but I don’t know if it’s usable (so far I haven’t tried it). Perhaps you should have another look at my CButtonColumn example (at least the label change will work). You can define a second button column if you want some separation from the default buttons.