Yii Framework Forum: Clistview + Cpagination Only Updates 1 Time - Yii Framework Forum

Jump to content

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

Clistview + Cpagination Only Updates 1 Time using CListView and CPagination with ajax Rate Topic: -----

#1 User is offline   newyiicoder 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-August 13

Posted 23 August 2013 - 02:38 PM

Hello, i started using yii a few days ago and i ran into a problem. I can't figure out how to get my CListView to update with ajax. I searched the forum and the internet for solutions, and i got a few things, but my code is still not working properly.

index view code:

                <div id="latestAdsContainerBottomAds">
			<?php $this->_getListViewAds(); ?>
		</div>
		<div id="latestAdsContainerBottomAdsPaginate">
			<?php $this->widget('CLinkPager', array(
                'pages' => $pages,
                'header'=>'',
				'prevPageLabel' => '&lt',
				'nextPageLabel' => '&gt',
                'footer'=>'</center>',
                'id'=>'link_pager'
                ))
			?>
		</div>
<script>
    $('ul.yiiPager a').click(function(ev){
        ev.preventDefault();
        $.get(this.href,{ajax:true},function(html){
             $('#latestAdsContainerBottomAds').html(html);
        });
    });
</script>

_index_paging_view view code:

[code]
<?php 
	$this->widget('zii.widgets.CListView', array(
		'id' => 'Ads',
		'dataProvider' => $dataProvider,
		'itemView' => '_view',
		'enablePagination'=>false,
		'pagerCssClass' => 'result-list',
		'summaryText' => '',
	));
?>


SiteController controller code:


public function actionIndex()
	{
		//get criteria
		$criteria = new CDbCriteria();
		$criteria->condition = 'typeID = :type';
		$criteria->params = array (':type'=> Ads::$selectedType);
		$criteria->order = 'id desc';
		 
		//get count
		$count = Ads::model()->count($criteria);
		 
		//pagination
		$pages = new CPagination($count);
		$pages->setPageSize(4);
		$pages->applyLimit($criteria);
		 
		//result to show on page
		$result = Ads::model()->findAll($criteria);
		$dataProvider = new CArrayDataProvider($result);
		
		$this->render('index', array(
			'dataProvider' => $dataProvider,
			'pages' => $pages
		));
	}

     public function _getListViewAds(){
		//get criteria
		$criteria = new CDbCriteria();
		$criteria->condition = 'typeID = :type';
		$criteria->params = array (':type'=> 1);
		$criteria->order = 'id desc';
		 
		//get count
		$count = Ads::model()->count($criteria);
		 
		//pagination
		$pages = new CPagination($count);
		$pages->setPageSize(4);
		//$thePage = $pages->getCurrentPage();
		//$pages->setCurrentPage($thePage+1);
		$pages->applyLimit($criteria);
		 
		//result to show on page
		$result = Ads::model()->findAll($criteria);
		$dataProvider = new CArrayDataProvider($result);
		
		$this->renderPartial('_index_paging_view', array(
			'dataProvider' => $dataProvider,
			'pages' => $pages
		));
    }


This way i get the first 4(1-4) results, and i can click next, i get the next 4(5-8), but after that i still get the results 5-8. I used firebug and i saw that page remains on page=2 on every next button click. I can click previous button and it gets me the first 4(1-4) results again. So it seems like that it is working for 1 next and 1 prev button click. I think i should change the CPagination currentPage variable in the controller. I tried a way: //$thePage = $pages->getCurrentPage(); //$pages->setCurrentPage($thePage+1); , but it's not working.
Maybe i have a problem with my MVC system, i'm new to this too.
Hope someone can help me out,
Cheers
0

#2 User is offline   softark 

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

Posted 23 August 2013 - 06:02 PM

Hi newyiicoder,

It's because the pager is not updated in ajax. You only replace the list part of the html.

BTW, why don't you use the pager of CListView? You could customize it to your taste somehow.
0

#3 User is offline   newyiicoder 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-August 13

Posted 24 August 2013 - 03:01 AM

View Postsoftark, on 23 August 2013 - 06:02 PM, said:

Hi newyiicoder,

It's because the pager is not updated in ajax. You only replace the list part of the html.

BTW, why don't you use the pager of CListView? You could customize it to your taste somehow.


Thanks a lot. That solved my problem. My _index_paging_view view file is now:

<?php 
	$this->widget('zii.widgets.CListView', array(
		'id' => 'Ads',
		'dataProvider' => $dataProvider,
		'itemView' => '_view',
		'enablePagination'=>false,
		'pagerCssClass' => 'result-list',
		'summaryText' => '',
	));
?>
<div id="latestAdsContainerBottomAdsPaginate">
<?php $this->widget('CLinkPager', array(
    'pages' => $pages,
    'header'=>false,
	'prevPageLabel' => '&lt',
	'nextPageLabel' => '&gt',
    'footer'=>'</center>',
    'id'=>'link_pager'
    ))
?>
</div>
			
<script>
$('ul.yiiPager a').click(function(ev){
    ev.preventDefault();
    $.get(this.href,{ajax:true},function(html){
        $('#latestAdsContainerBottomAds').html(html);
    });
});
</script>


and this way my actionIndex is only $this->render('index'); . So i have no duplication in my code.
Thanks again,
Cheers
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