您可以使用会话/cookie 来存储检查的值。我不太确定如何使 cookie 发挥作用,所以我将告诉您如何通过会话来做到这一点。特别是 yii 创建的用户会话。
现在要使用会话,我们需要将已检查(和未检查)的 id 传递给控制器,因此我们将在每次 ajax 更新(即分页之间)时修改发送到控制器的数据,为此,我们利用beforeAjaxUpdate
的选项网格视图.
我也在用CCheckBoxColumn instead您的代码中包含以下内容(当然您可以修改解决方案以满足您自己的需要):
array(
'name' => 'demo',
'type'=>'raw',
'header' => "Select",
'value' => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
),
网格视图更改:
<?php $this->widget('zii.widgets.grid.CGridView', array(
// added id of grid-view for use with $.fn.yiiGridView.getChecked(containerID,columnID)
'id'=>'first-grid',
'dataProvider'=>$model->search(),
'cssFile' => Yii::app()->baseUrl . '/media/js/admin/css/admingridview.css',
// added this piece of code
'beforeAjaxUpdate'=>'function(id,options){options.data={checkedIds:$.fn.yiiGridView.getChecked("first-grid","someChecks").toString(),
uncheckedIds:getUncheckeds()};
return true;}',
'ajaxUpdate'=>true,
'enablePagination' => true,
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'name' => 'fb_user_id',
'header' => 'FaceBook Id',
'value' => 'CHtml::encode($data->fb_user_id)',
),
array(
'name' => 'first_name',
'header' => 'Name',
'value' => 'CHtml::encode($data->first_name)',
),
array(
'name' => 'email_id',
'header' => 'Email',
'value' => 'CHtml::encode($data->email_id)',
),
/* replaced the following with CCheckBoxColumn
array(
'name' => 'demo',
'type'=>'raw',
'header' => "Select",
'value' =>'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
),
*/
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => '2',
'header'=>'Selected',
'id'=>'someChecks', // need this id for use with $.fn.yiiGridView.getChecked(containerID,columnID)
'checked'=>'Yii::app()->user->getState($data->email_id)', // we are using the user session variable to store the checked row values, also considering here that email_ids are unique for your app, it would be best to use any field that is unique in the table
),
),
));
?>
特别注意以下代码beforeAjaxUpdate
和 CCheckBoxColumn,在 beforeAjaxUpdate 中我们传递checkedIds
作为已检查的所有 id(在本例中为 email_ids)的 csv 字符串uncheckedIds
作为所有未选中 id 的 csv 字符串,我们通过调用函数获取未选中的框getUncheckeds()
,随后不久。请注意,当我测试时,我使用了(我的表中的)整数 id 字段作为唯一字段,而不是电子邮件字段。
The getUncheckeds()
函数可以像这样在 gridview 的视图文件中的任何位置注册:
Yii::app()->clientScript->registerScript('getUnchecked', "
function getUncheckeds(){
var unch = [];
/*corrected typo: $('[name^=someChec]') => $('[name^=someChecks]') */
$('[name^=someChecks]').not(':checked,[name$=all]').each(function(){unch.push($(this).val());});
return unch.toString();
}
"
);
在上面的函数中,请注意选择器和each
and push
功能。
完成后,我们需要修改该视图的控制器/操作。
public function actionShowGrid(){
// some code already existing
// additional code follows
if(isset($_GET['checkedIds'])){
$chkArray=explode(",", $_GET['checkedIds']);
foreach ($chkArray as $arow){
Yii::app()->user->setState($arow,1);
}
}
if(isset($_GET['uncheckedIds'])){
$unchkArray=explode(",", $_GET['uncheckedIds']);
foreach ($unchkArray as $arownon){
Yii::app()->user->setState($arownon,0);
}
}
// rest of the code namely render()
}
就是这样,现在应该可以工作了。