Store CGridView state (CSort, CPagination and CDbCriteria)

I just created all the CRUD operation using gii. To make the CGridView a bit more user friendly I like to store the CPagination, CSort and CDbCriteria objects in a session or cookie so when the user comes back to the page, I can restore the CGridView is in the same state as when the user left the page.

When I try the catch the object in the action search() is doesn’t get the new CSort, CPagination or CDbCriteria parameters after sorting, filtering or navigating the CGridView.




    public function search() {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria = new CDbCriteria;


        $criteria->compare('id_city', $this->id_city);

        $criteria->compare('country', $this->country, true);

        $criteria->compare('province', $this->province, true);

        $criteria->compare('municipality', $this->municipality, true);

        $criteria->compare('city', $this->city, true);

        $criteria->compare('deeplink', $this->deeplink, true);

        $criteria->compare('latitude', $this->latitude);

        $criteria->compare('longitude', $this->longitude);


        $criteria->with = array('country');


        $dp = new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

            'pagination'=>array(

                //'currentPage'=>$page,

                'pageSize'=>Yii::app()->params['itemsPerPage'],

            ),

            'sort' => array(

                'defaultOrder' => 'id_city',

                'attributes' => array(

                    '*',

                    'country' => array(

                        'asc' => 'country.country',

                        'desc' => 'country.country DESC',

                    )

                )

            ),

        ));

        

        //save page, order, filter

        $par['pagination'] = $dp->getPagination();

        $par['criteria'] = $dp->getCriteria();

        $par['sort'] = $dp->getSort();


        $cookie = new CHttpCookie('city-grid', serialize ($par));

        $cookie->expire = time()+60*60*24*180;


        Yii::app()->request->cookies['city-grid'] = $cookie;


        if(!$_GET) {

            $cookie = Yii::app()->request->cookies['city-grid'];


            $cookie = unserialize($cookie->value);


            $dp->setPagination($cookie['pagination']);

            $dp->setCriteria($cookie['criteria']);

            $dp->setSort($cookie['sort']);

        }


        return $dp;

    }



nobody?

Maybe this could be of help?

http://www.yiiframework.com/forum/index.php?/topic/8994-dropdown-for-pagesize-in-cgridview

it was helping a bit. But in ran into troubles when clicking the ‘1’ link of the pager. The action behind this link is not sending the Daytrip_page get parameter.

For now the user can not navigate to page 1… because the script in the controller will not react and will not set the page back to 0…

any ideas?

controller:


        if (isset($_GET['Daytrip_page'])) {

            Yii::app()->user->setState('page', (int) $_GET['Daytrip_page'] - 1);

            unset($_GET['Daytrip_page']);

        }

view:


            'pagination' => array(

                'currentPage'=>Yii::app()->user->getState('page', 0)

            ),

any ideas?

How about storing the state of the CGridView in a URL such as


http://10.41.208.51/myWebApp/index.php?r=status%2Fadmin&Status%5BparameterName%5D=&Status%5BdeviceName%5D=&Status%5BdeviceIpAddress%5D=&Status%5BparameterValue%5D=&ajax=status-grid&Status_sort=deviceIpAddress&isInterval=true&Status_page=6

This may simplify things. The example URL above was generated by CGridView to be used for Ajax requests to update the page. In the example URL above, the model is Status and the columns are parameterName, deviceName, deviceIpAddress, and parameterValue.