Anton Scherbinin
Anton Scherbinin

Reputation: 57

Yii pagination with ajax

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?

Upvotes: 4

Views: 8272

Answers (2)

Ulug&#39;bek
Ulug&#39;bek

Reputation: 2832

@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

Upvotes: 2

Let me see
Let me see

Reputation: 5094

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);
                }

        })

Upvotes: 2

Related Questions