php分页保留多选框的值,php – 保留Yii gridview分页中的复选框值

您可以使用会话/ cookie来存储选中的值.我不太确定如何使cookie工作,所以我会告诉你如何使用会话.特别是yii创建的用户会话.

现在使用会话我们需要将已检查(和未经检查)的id传递给控制器​​,因此我们将修改每次ajax更新(即分页之间)发送到控制器的数据,为此我们利用beforeAjaxUpdate选项CGridView.

我也在你的代码中使用CCheckBoxColumn而不是以下内容(当然你可以根据自己的需要修改解决方案):

array(

'name' => 'demo',

'type'=>'raw',

'header' => "Select",

'value' => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',

),

GridView更改:

<?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的csv字符串传递(在本例中为email_ids)已经检查过并且uncheckedIds为所有未经检查的id的csv字符串,我们得到了通过调用函数getUncheckeds()来取消选中框,紧接着就是这样.请注意,当我测试时,我使用了一个整数id字段(我的表)作为唯一字段,而不是电子邮件字段.

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();

}

"

);

在上面的功能中要注意选择器和每个推送功能.

完成后,我们需要修改此视图的控制器/操作.

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()

}

就是这样,它现在应该可以工作了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值