I am trying to add a checkbox into a column in gridview so when user click on the checkbox it make call to controller to toggle a value of the record.
The code below works only at the first page of a gridview. When I click on a checkbox in let say 2nd row of the first page of gridview then I see the controller receives the id of the second row listed on the first page.
But if I click "next" and go to the second page and try to click checkbox of let say 4-th row of the second page the controller receives the id of the 4-th row of the FIRST page. And does not matter what page I am on click on checkbox sends the id of the row of first page.
The code from the view looks like this:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'topstudent-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'schoolname',
'studid',
'senttext',
array(
'header'=>'Do Send',
'class'=>'CDataColumn',
'type'=>'raw',
'htmlOptions'=>array('style'=>'text-align:center'),
'value' => 'CHtml::checkBox("active",
$data->active,
array("submit"=>"index.php?r=topstudent/toggleactive",
"params"=>array("id"=>$data->id,"active"=>$data->active)))',
),
),
)); ?>
And the controller code like next (although nothing wrong with controller so far, the problem it receives incorrect id in the $_POST):
public function actionToggleActive() {
print_r($_POST);
/***
$id = $_POST['id'];
if(!empty($id)) {
$active = (empty($_POST['active']) || $_POST['active'] == '0') ? 1 : 0 ;
$model=$this->loadModel($id, 'active');
$model->active = $active;
$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 {
echo 'Invalid request... no id defined.';
}
***/
}