Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:08:42+00:00 2026-06-01T23:08:42+00:00

I have a gridview which contains a checkbox column and also uses pagination. When

  • 0

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?

Code for Gridview is

$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))',
        ),
    ),
));

Edit:

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

Thanks to bool.dev

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:08:43+00:00Added an answer on June 1, 2026 at 11:08 pm

    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.

    Now to use sessions we need to pass the checked (and unchecked) ids to the controller, therefore we’ll modify the data being sent to the controller on every ajax update(i.e between paginations), to do this we exploit the beforeAjaxUpdate option of CGridView.

    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 Changes:

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

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview which contains few Template columns, In the first Template column
I have a button, a usercontrol which contains a gridview in a page. How
I have a gridview which contains checkbox and paging is enabled. Now when I
I have a gridview which contains controls like checkbox dropdownlist textbox etc.. These controls
I have a gridview which contains checkboxes and fields in sql server database which
I have user control on a ASP.NET web page, which contains a GridView and
I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like
For example, Lets say that I have a gridview column which has important controls,
Hello I have created and application which contains a gridview that is populated by
A typical situation: In my GridView control, I have a Footer row which contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.