php - 在 Yii gridview 分页中保留复选框值

标签 php ajax pagination yii

我有一个 gridview,它包含一个复选框列并且还使用了分页。当我选中第一页中的一些复选框并导航到第二页并在第二页中选中另一个复选框时,我在第一页中选中的选项不会保留在那里。是否可以在分页期间保留复选框值?

Gridview 的代码是

$widget = $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'     => $model->search(),
    'cssFile'          => Yii::app()->baseUrl . '/media/js/admin/css/admingridview.css',
    //'filter' => $model,
    'ajaxUpdate'       => true,
    'enablePagination' => true,
    'columns'          => array(
        array(
            'name'   => 'id',
            'header' => '#',
            'value'  => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
        ),
        array(
            'class'          => 'CCheckBoxColumn',
            'selectableRows' => '2',
            'header' => 'Selected',
        ),
        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)',
        ),
        array(
            'name'   => 'demo',
            'type'   => 'raw',
            'header' => "Select",
            'value'  => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
        ),
    ),
));

编辑:

用于记住 gridview 中选定选项的扩展,检查此链接 Selgridview

感谢 bool.dev

最佳答案

您可以使用 session /cookie 来存储选中的值。我不太确定如何使 cookie 起作用,所以我将告诉您如何使用 session 来做到这一点。特别是 yii 创建的用户 session 。

现在要使用 session ,我们需要将选中(和未选中)的 ID 传递给 Controller ​​,因此我们将在每次 ajax 更新时(即分页之间)修改发送到 Controller 的数据,为此我们利用CGridViewbeforeAjaxUpdate 选项.

我也在使用 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(在本例中为 email_ids)的 csv 字符串传递被检查并且 uncheckedIds 作为所有未检查的 id 的 csv 字符串,我们通过调用函数 getUncheckeds() 获得未检查的框,紧随其后。请注意,当我测试时,我使用了一个整数 id 字段(我的表)作为唯一字段,而不是电子邮件字段。

getUncheckeds() 函数可以像这样在 gridview 的 View 文件中的任何地方注册:

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

在上面的函数中,请注意选择器以及eachpush 函数。

完成后,我们需要修改此 View 的 Controller /操作。

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

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

关于php - 在 Yii gridview 分页中保留复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099867/

相关文章:

javascript - 如何创建秒表

PHP 搜索数据库和目录结构并匹配在一起

javascript - 使用AJAX、PHP和MySQL显示表格数据

javascript - 如何使用ajax从一组函数中调用单个函数?

javascript - Jquery 中的动态分页

php - 如何修复 Laravel 8 UI 分页问题?

javascript - AngularJS 分页和无法读取未定义的属性 'slice'

php - Laravel Blade 如何使用 if 语句为空或空

php - 快速实体主义水化器

php - 为什么 R::Store 不返回任何东西并在 redbeanphp 中崩溃?