Yii pagination with ajax
ajax, javascript, jquery, php, yii
Solution
In your controller do this
if (Yii::app()->request->isAjaxRequest) {
$done =$this->renderPartial('index', array('dataProvider' =>
$dataProvider), true);
echo $done;
Yii::app()->end();
}
And change your ajax call to this
$.ajax({
url: 'index.php/news',
type: "POST",
dataType: "html",
success: function(data){
$('#news').html(data);
}
})
Problem
I need to enable pagination with ajax my code Controller(update content ajax) ``` function actionIndex(){ $dataProvider=new CActiveDataProvider('News', array( 'pagination'=>array( 'pageSize'=>1, ), )); if (Yii::app()->request->isAjaxRequest) { $done =$this->renderPartial('index', array('dataProvider' => $dataProvider), true); echo CJSON::encode($done); Yii::app()->end(); } $this->render('index', array( 'dataProvider'=>$dataProvider, )); } ``` JS (on click event show renderpartial) ``` $(document).ready(function(){ $(".menunews").click(function() { $( "body" ).addClass( "news" ); $.ajax({ url: 'index.php/news', type: "GET", dataType: "JSON", success: function(data){ $('#news').append(data); } }).fail(function(){ alert("Error"); }); }); ``` view ``` <div class="newscont"> <h1>News</h1> <?php $this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view', 'template'=>"{items}\n{pager}", 'ajaxUpdate'=>'true', 'enablePagination'=>true, 'pager' => array( 'firstPageLabel'=>'<<', 'prevPageLabel'=>'<', 'nextPageLabel'=>'>', 'lastPageLabel'=>'>>', 'maxButtonCount'=>'10', 'header'=>'<span>Pagination</span>', 'cssFile'=>Yii::app()->getBaseUrl(true).'/themes/phil/css/pager.css', ), )); ?> </div> ``` But if i load page throw renderpartial using ajax my pagination doesn't work, how i can fix it?