Wednesday, April 18, 2012

Set default page in Clistview or CGridview in the last page yiiframework

By default default page of Clistview or Cgridview only set to the front of clistview or in the first page as default page. But when you want to use the last page as your default page to be viewed to user you have to modified the pagination in the data provider. You can use this to show when you are using the clistview or component that has the pagination object like others. Maybe you can use this as default discussion thread.

Let's take a look the code that you should have to write down.

if(Yii::app()->request->isAjaxRequest){
 $dataProvider=new CActiveDataProvider('KomentarThread',array(
   'criteria'=>array(
     'condition'=>'ID_THREAD=:id_thread',
     'params'=>array(':id_thread'=>$model->ID_THREAD)
   )
 ));
 $p=$dataProvider->pagination;
 $p->setItemCount($dataProvider->getTotalItemCount());
 $p->currentPage=$p->pageCount-1;
 $this->renderPartial('/komentarThread/_comments_all',array(
   'all_komentar'=>$dataProvider)
   ,false,true);
 Yii::app()->end();
}

The part of $p variable is to set your currentPage to the last page.

So enjoy your modification of your data provider...

2 comments:

  1. Replies
    1. setting the current page seems to prevent me from clicking to other pages?

      Delete