Yii Framework Forum: Strange Behavior Chtml::checkbox In Gridview, Need Help. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Strange Behavior Chtml::checkbox In Gridview, Need Help. CHtml::checkBox posts wrong record id from a gridview Rate Topic: -----

#1 User is offline   Vovik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-February 13

Posted 23 February 2013 - 04:36 PM

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.';
                }
***/
	}

0

#2 User is offline   Vovik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-February 13

Posted 24 February 2013 - 01:08 PM

Resolved.

I did not get an answer from an expert on this forum but if anybody have similar problem I have found a solution here:

:)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users