Yii Framework Forum: Cpagination Only Works On First Page - Yii Framework Forum

Jump to content

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

Cpagination Only Works On First Page Recieving POST variables from a form makes CPaginator losing the data Rate Topic: -----

#1 User is offline   benelli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 14

Posted 29 May 2014 - 10:04 AM

Hello all, i am new to Yii and this forum. I am starting a new project and after some researchs i have decided to learning this framework and use it.

Now i am stuck with the pagination. I have a form, sending by POST only 2 variables to an action. This action generates the pagination, and the first page is shown well, but if i try to navigate through the CLinkPager there is no data shown, even the CLinkpager itself has disappeared.

I have 3 days lost yet with this problem. I have read almost every pagination related post without finding a solution, and i have changed my code almost 100 times. First i tried to paginate from a model function directly (that returned a CActiveDataProvider) but i was unable to even show the first page. I have seen all the pagination examples triggered from the controller, i mean always the querys are executed from the actions in the controller (in order to paginate the data).

There is no way to paginate a CActiveDataProvider object directly, for example returned from a model function?

Now the code is this, works for only first page:

Controller:

public function actionIndexSearch() {

        //here some trivial validations and a debug echo 
        if ($_POST['city'] != null && isset($_POST['type'])) {
            //I tried also defining constants for ensure the data is not lost by pagination (maybe recalling this action again without POST data...)
            //define(_CITY, $_POST['city']);
            //define(_TYPE, $_POST['type']);
            $cityId = $_POST['city'];
            $type = $_POST['type'];
            echo "dentro";
        }
        //Another echo for watching the behavior.
        echo "fuera";
        //Creating the data to paginate and its pagination:
        $c = new CDbCriteria();
        $location = Location::model()->tableName();
        $c->compare('t.event_type_id', $type, false);
        $c->join = 'inner join ' . $location . ' l on l.city_id = "' . $cityId . '"';
        $itemcount = LiveEvent::model()->count($c);
        $pages = new CPagination($itemcount);
        $pages->setPageSize(2);
        $pages->applyLimit($c);

        Yii::app()->setTheme('urbandow');
        $this->layout = 'main';
        $this->render('eventsResults', array(
            'data' => LiveEvent::model()->findAll($c),
            'pages' => $pages,
            //'cityId' => $cityId,
            //'type' => $type,
        ));
    }



In my view, for example:


 <?php
                                $this->widget('CLinkPager', array(
                                'pages' => $pages,
                                    ));
?> 

<?php
                    
                    
                    foreach ($data as $data1){
                          echo $data1->title;
                    }
                    
?>  



Ok, and in my model i had this (now it is not used, but i think if we want put all the logic in the models there should be a way to paginate it...):

public static function listByCity($cityId, $type) {

        $c = new CDbCriteria();

        $location = Location::model()->tableName();
        $c->compare('t.event_type_id', $type, false);
        $c->join = 'inner join ' . $location . ' l on l.city_id = "' . $cityId .'"';
        return new CActiveDataProvider($this, array(
            'criteria' => $c,
            'pagination' => 2,
        ));
    }


Thank you very much!
0

#2 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 883
  • Joined: 19-February 13
  • Location:India

Posted 31 May 2014 - 12:21 PM

just past the proceeOutput when page is render...

for ex..
$this->render('index',array(),false,true);

Thanks,
Ankit Modi
Skype : amodi06
0

#3 User is offline   benelli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 14

Posted 02 June 2014 - 04:32 AM

Hello again.

I have tried it Ankit, if i use TRUE the page is not rendered. If i use FALSE the behavior doesn't change.

http://www.yiiframew...r#render-detail

If i declare the variables $cityId=1 and $type=1 inside the action method instead of retrieving the values from the post data, the pagination works well in all pages. I mean the data is lost when the pagination buttons are used, because probably the method actionIndexSearch() is called again, and its called without recieving any post data, so the new page have no data and is shown empty (and without paginator).

How is this solved in Yii? How can i use a form to get some data and then paginate it?
0

#4 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 883
  • Joined: 19-February 13
  • Location:India

Posted 02 June 2014 - 11:03 PM

View Postbenelli, on 02 June 2014 - 04:32 AM, said:

Hello again.

I have tried it Ankit, if i use TRUE the page is not rendered. If i use FALSE the behavior doesn't change.

http://www.yiiframew...r#render-detail

If i declare the variables $cityId=1 and $type=1 inside the action method instead of retrieving the values from the post data, the pagination works well in all pages. I mean the data is lost when the pagination buttons are used, because probably the method actionIndexSearch() is called again, and its called without recieving any post data, so the new page have no data and is shown empty (and without paginator).

How is this solved in Yii? How can i use a form to get some data and then paginate it?


Try this jquery.js fals when action was called



$cs = Yii::app()->clientScript;

$cs->reset();

$cs->scriptMap = array(

'jquery.js' => false, // prevent produce jquery.js in additional javascript data

);
Thanks,
Ankit Modi
Skype : amodi06
0

#5 User is offline   benelli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 14

Posted 03 June 2014 - 08:14 AM

View PostAnkit Modi, on 02 June 2014 - 11:03 PM, said:

Try this jquery.js fals when action was called



$cs = Yii::app()->clientScript;

$cs->reset();

$cs->scriptMap = array(

'jquery.js' => false, // prevent produce jquery.js in additional javascript data

);


Hello again, thanks for the reply! But it hasn't solved my problem :(

I tried also disable al jquery in the page without success.

I still searching the way to paginate with Yii :(
0

#6 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 883
  • Joined: 19-February 13
  • Location:India

Posted 04 June 2014 - 06:27 AM

Hi,

can you please post the admin view?
Just conform you can use the Clink Pager or not?

Thanks,
Ankit Modi
Skype : amodi06
0

#7 User is offline   benelli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 14

Posted 04 June 2014 - 10:37 AM

View PostAnkit Modi, on 04 June 2014 - 06:27 AM, said:

Hi,

can you please post the admin view?
Just conform you can use the Clink Pager or not?


I attach the file, the line 107 is where i implement the pagination, thanks!

Attached File(s)


0

#8 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 883
  • Joined: 19-February 13
  • Location:India

Posted 05 June 2014 - 11:44 PM

Hi Man,

As a friend I think your code is not correct and it's not proper way..

please don't keep in mind..Posted Image

Please use this proper Yii syntax..

http://www.yiiplaygr...taview/gridView


if you have any query please contact me on Skype : amodi06
if I am available I will help you.


Regards,
Ankit Modi
Thanks,
Ankit Modi
Skype : amodi06
0

#9 User is offline   benelli 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-May 14

Posted 03 July 2014 - 06:26 AM

Hello Again.

Finally i didn't understand how to use the Yii pagination alone (without any other helper like gridview), so i have solved the pagination problem creating it by AJAX from scratch by my self.

Thanks anyway!
0

#10 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 876
  • Joined: 20-April 11
  • Location:India

Posted 03 July 2014 - 07:32 AM

This is easy to implement.

In controller or in the same view

This is Just an example.

                $criteria = new CDbCriteria;
		$criteria->compare('is_deleted',0);  // normal DB field
                $criteria->order = 'first_name ASC';
		
		$total = Students::model()->count($criteria);
		$pages = new CPagination($total);
        $pages->setPageSize(Yii::app()->params['listPerPage']);
        $pages->applyLimit($criteria);  // the trick is here!
		$posts = Students::model()->findAll($criteria);
		
		 
		$this->render('manage',array('model'=>$model,
		'list'=>$posts,
		'pages' => $pages,
		'item_count'=>$total,
		'page_size'=>Yii::app()->params['listPerPage'],)) ;







In view file
<?php 
                              $this->widget('CLinkPager', array(
                              'currentPage'=>$pages->getCurrentPage(),
                              'itemCount'=>$item_count,
                              'pageSize'=>$page_size,
                              'maxButtonCount'=>5,
                              //'nextPageLabel'=>'My text >',
                              'header'=>'',
                            'htmlOptions'=>array('class'=>'pages'),
                            ));?>

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#11 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 876
  • Joined: 20-April 11
  • Location:India

Posted 03 July 2014 - 07:34 AM

Avoid POST use GET
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
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