Yii Framework Forum: Cactivedataprovider Not Switching Pages - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cactivedataprovider Not Switching Pages Rate Topic: -----

#1 User is offline   wicked357 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-September 11

Posted 23 August 2013 - 03:36 PM

CActiveDataProvider is being used to create a paginated list of results from a search form I made.

First page works properly but if I click on page 2 for example, it clears the CActiveDataProvider instead of switching results. Can anyone tell me what is happening.

function actionSearch() {
   if (!empty($_POST['id'])) {
       $searchResults = $this->search($_POST);
   }
   // render to view with $searchResults
}

function search($postData) {
   // Search using the $postData in $criteria

   // Creates a 5 page pagination CActiveDataProvider using the $criteria

   return $dataProvider;
}


Sorry for the minimal code I am trying to simply what to look at this is the basic algoritm for the search method and how it builds the CActiveDataProvider.

Since I built a basic pagination in a sandbox app for testing I didn't pass variables in a POST form, so that worked as expected, but now that I have used it in a search where I pass variables in POST I am not sure how to get past this issue. Thanks in advance for the help.
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 23 August 2013 - 04:06 PM

You should show us how do you configure the pagination in the dataProvider.
Don't be a dick.
0

#3 User is offline   wicked357 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-September 11

Posted 23 August 2013 - 04:13 PM

            $dataProvider = new CActiveDataProvider($modelName, array(
                'criteria'=>$criteria,
                'pagination'=>array(
                    'pageSize'=>5,
                ),
            ));


        $this->widget('zii.widgets.CListView', array(
            'dataProvider'=> $dataProvider,
            'id' => 'results-list-view',
            'itemView'=>'_results',
            'pagerCssClass' => false,
            'pager' => array(
                'cssFile' => '/css/styles.css',
                'header' => false,
                'prevPageLabel' => 'Prev',
                'nextPageLabel' => 'Next',
            ),
        ));

0

#4 User is offline   wicked357 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-September 11

Posted 23 August 2013 - 05:38 PM

Here is an isolated code that does the same exact thing my site is doing...

(Controller) Index action and search function
	public function actionIndex()
	{
        $dummyDataProvider = null;
        if (!empty($_POST['title'])) {
            $postData = $_POST;
            $dummyDataProvider = $this->search($postData);
        }
        $this->render('index', array(
            'dummyDataProvider' => $dummyDataProvider,
        ));
	}

    public function search($data=null) {
        $model = Dummy::model();
        $criteria = new CDbCriteria();
        $criteria->order = 'id ASC';
        $criteria->condition = 'title LIKE \'%' . $data['title'] . '%\'';
        return new CActiveDataProvider($model, array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>1,
            ),
        ));
    }


(View)
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::textField('title', '', array('class' => 'span8', 'placeholder' => 'enter letter')); ?>
<?php echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::endForm(); ?><?php
    if ($dummyDataProvider) {
        $this->widget('zii.widgets.CListView', array(
            'dataProvider'=> $dummyDataProvider,
            'id' => 'dummy-list-view',
            'itemView'=>'_dummyList',
            'template' => '
                            <div class="span9 pull-right">
                                <div class="pagination pagination-right">
                                    {pager}
                                </div>
                           </div>
                            <div style="border-bottom:1px solid #000;overflow:hidden;margin-bottom:5px;">
                                <div style="float:left;width:100px;text-align:center;">ID:</div>
                                <div style="float:left;width:100px;text-align:center;">Title:</div>
                                <div style="float:left;width:100px;text-align:center;">Action:</div>
                            </div>
                           {items}',
            //'cssFile' => falses,
            'pager' => array(
                'header' => false,
                //'cssFile' => false,
                'prevPageLabel' => 'Prev',
                'nextPageLabel' => 'Next',
            ),
            'sortableAttributes'=> array (
                'id'=>'ID',
                'title' => 'Title',
                'action' => 'Action',
            ),
        ));
    }
?>

0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 23 August 2013 - 05:46 PM

Hi wicked357,

Use get method.
CListView handles pagination and sorting by get method.

http://www.yiiframew...ivedataprovider
1

#6 User is offline   wicked357 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-September 11

Posted 23 August 2013 - 06:19 PM

I got it to work by setting this...

$dummyDataProvider->getPagination()->params = array("title" => $post['title'], 'test' => 'test');

then as mentioned by softark using $_GET if $_POST isn't populated in my action.

Problem: In my example version it does it without posting those attributes to the url, in my website it is posting all of them in the url but it works. I would prefer the cleaner look.

Any ideas what would cause this?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users