保留在Yii中gridview的分页复选框值分页、复选框、Yii、gridview

2023-09-10 16:07:34 作者:11.菁华浮梦长

我有一个GridView,其中包含一个复选框列,并且也使用pagination.When我签在第一页中的一些复选框并导航到第二页,并检查另一个在第二页,我检查的第一页上的选项是不保留there.Is它posssible保留分页中的复选框值????

I have a gridview which contains a checkbox column and also uses pagination.When i check some checkboxes in the first page and navigate to the second page and check another one in the second page,The options i checked in the first page is not retained there.Is it posssible to retain the checkbox values during pagination????

$ C $下的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

Extension for remembering the selected options in gridview,check this link Selgridview

由于的 bool.dev 的

推荐答案

您可以使用会话/ Cookie存储的检查值。我不是很清楚如何做饼干的工作,所以我会告诉你如何使用会话做到这一点。特别是在用户会话的Yii造成的。

You could use sessions/cookies to store the checked values. I'm not very sure how to make cookies work, so i'll tell you how to do it with sessions. Specifically the user session that yii creates.

现在使用我们需要会话的检查(和未选中)IDS传递给控制器​​,因此我们将修改数据被发送到控制器上的每一个AJAX更新(即paginations之间),要做到这一点,我们利用 beforeAjaxUpdate 中的CCheckBoxColumn 而不是在code以下(当然你可以修改的解决方案,以满足自己的需要):

I'm also using CCheckBoxColumn instead of the following in your code(of course you can modify the solution to suit your own needs):

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
            ),
    ),
));
?>

要特别注意$ C $下 beforeAjaxUpdate 和CCheckBoxColumn,在beforeAjaxUpdate我们传递 checkedIds 作为所有的IDS(在这种情况下email_ids)已签和 uncheckedIds 为所有选中ID的CSV字符串,我们得到的未经检查的箱子通过调用一个CSV字符串功能 getUncheckeds(),随后不久。请注意这里,当我在测试我用了(我的表)一个整数ID字段作为唯一的字段,而不是电子邮件域。

Pay special attention to the code for beforeAjaxUpdate and CCheckBoxColumn, in beforeAjaxUpdate we are passing checkedIds as a csv string of all the ids(in this case email_ids) that have been checked and uncheckedIds as a csv string of all the unchecked ids, we get the unchecked boxes by calling a function getUncheckeds(), which follows shortly. Please take note here, that when i was testing i had used an integer id field (of my table) as the unique field, and not an email field.

getUncheckeds()函数可以注册像这样的任何地方查看文件GridView的:

The getUncheckeds() function can be registered like this anywhere in the view file for 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();
       }
       "
);

在上面的功能要注意选择器和每个的功能。

In the above function pay attention to the selectors and each and push function.

做完这些,我们需要修改控制器/行动这一观点。

With that done, we need to modify the controller/action for this view.

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

这就是它,它现在应该工作。

That's it, it should work now.