栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Yii GridView分页中保留复选框值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Yii GridView分页中保留复选框值

您可以使用会话/ cookie来存储检查的值。我不太确定如何使cookie正常工作,因此我将告诉您如何在会话中进行操作。特别是yii创建的用户会话。

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

beforeAjaxUpdate
选项。

我还在代码中使用了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 pre    '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::enpre($data->fb_user_id)', ), array(      'name' => 'first_name',      'header' => 'Name',      'value' => 'CHtml::enpre($data->first_name)', ), array(      'name' => 'email_id',      'header' => 'Email',      'value' => 'CHtml::enpre($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和CCheckBoxColumn
的代码,在beforeAjaxUpdate中,我们将
checkedIds
所有已检查的id(在本例中为email_ids)的csv字符串作为传递,而
uncheckedIds
作为所有未检查的id
的csv字符串传递,我们将未检查的框通过调用一个函数
getUncheckeds()
,随后不久。请在这里注意,当我测试时,我使用(我表的)整数id字段作为唯一字段,而不是电子邮件字段。

getUncheckeds()
可以像这样在gridview的视图文件中的任何位置注册该功能:

Yii::app()->clientscript->registerscript('getUnchecked', "       function getUncheckeds(){ var unch = [];  $('[name^=someChecks]').not(':checked,[name$=all]').each(function(){unch.push($(this).val());}); return unch.toString();       }       ");

在上面的函数中,请注意选择器and

each
push
函数。

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

public function actionShowGrid(){     // some pre already existing     // additional pre 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 pre namely render()}

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485287.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号