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?
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?
Share Improve this question asked Apr 2, 2014 at 9:21 Anton ScherbininAnton Scherbinin 571 silver badge7 bronze badges 4- I am not pletly sure, but i think you should not return JSON, but simply HTML and the ListView will do the rest – Asped Commented Apr 2, 2014 at 10:03
- or if you do the wholw thing by your own.. forget the ajaxUpdate function, and handle the whole request. I think you do somthing of both, you have to deicide if you want to use the automatic pagination of the listview, or make the whole paging yourself – Asped Commented Apr 2, 2014 at 10:06
- thank you for response but how i can return html? it is not correctly i think, now i am trying to change CListView js to update page but it is not work – Anton Scherbinin Commented Apr 2, 2014 at 10:14
- 1 you can simply return HTML when you leave out the JSON::encode. And set in your script dataType:HTML – Asped Commented Apr 2, 2014 at 10:34
2 Answers
Reset to default 2In 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);
}
})
@Let me see's answer is right also, but the second way is that you can use like that:
if (Yii::app()->request->isAjaxRequest) {
$done =$this->renderPartial('index', array('dataProvider' => $dataProvider), true);
echo CJSON::encode(array(
'status' => 'success',
'html' => $done,
));
Yii::app()->end();
}
$.ajax({
url: 'index.php/news',
type: "GET",
dataType: "JSON",
success: function(data){
$('#news').append(data.html);
}
});
It is right way to get a JSON
from server, or you want to get HTML
response use @Let me see's answer