Yii ClinkPager不会处理Ajax请求

时间:2014-03-20 06:53:33

标签: ajax yii pagination

首先,当页面加载时,ClinkPager正常工作,正确显示所有分页。 但是,当发送Ajax请求时,结果会在所有分页中正确填充。 但是,如果单击分页中的下一页或另一页,则会加载先前的数据,并且分页也会显示不同的顺序。

/*Controller action to fetch the records and apply the pagination*/
//---------------------------------------------------------------
public function actionGetUser($user_id=null)
{
$user_domain= (isset($_POST['user_domain'])?$_POST['user_domain']: null);

$model=new UserSearch();
$criteria=new CDbCriteria();
//If Category/Title are also specified for search, then its an Ajax request.
if((isset($_POST['ajax_search'])) && ($_POST['ajax_search']==1))
{
    //Change the search criteria accordingly
    $criteria->select="*";

            if($user_domain!= null)
    {
                //Adding criteria to search for ideas of specific domain
        $criteria->addCondition("user_domain=".$usr_domain);
    }
}
    //Retrieve the users.
$searchData = $model->search();

//Count the no. of results retrieved.
$count=UserSearch::model()->count($criteria);

//Enable pagination
$pages=new CPagination($count);
$searchData->setPagination($pages);
$pages->applyLimit($criteria);

//Search for ideas satisfying that criteria
$models=userSearch::model()->findAll($criteria);

    if((isset($_POST['ajax_search'])) && ($_POST['ajax_search']==1))
{
    //Rendering the respective page
    $this->renderPartial('renderOnAjax', array(
                'user' => $models,
                            'pages' => $pages,
            'user_count'=>$count
        ));
    }
    else
{
    //Rendering the respective page
    $this->render('render', array(
                'user' => $models,
                'pages' => $pages,
            'user_count'=>$count
        ));
}
}
//------------------------------------------------------------

/*render page*/
//------------------------------------------------------------
<div>
<div class="userInfo" id="user_search_result">
    <?php $this->renderPartial("renderOnAjax",array('user'=>$user, 'pages'=>$pages));?>
</div>
</div>
//------------------------------------------------------------

/*renderOnAjax Page*/
//------------------------------------------------------------
<?php 
$i=0; 
$count=count($user);?>
<?php while($i!=$count) {?>
<?php $row=$count[$i];?>
<div class="Box">
/*Some contain to display...*/

</div>
<?php $i++;?>
<?php } ?>

<div class="row">
    <?php  $this->widget('CLinkPager', array(
            'pages' => $pages
        ));
    ?>
</div>   

// --------------------------------------------- ------------

1 个答案:

答案 0 :(得分:0)

尝试

<?php $this->renderPartial("renderOnAjax",array('user'=>$user, 'pages'=>$pages),false,true);?>

这是renderPartial的正式文件

public string renderPartial(string $view, array $data=NULL, boolean $return=false, boolean $processOutput=false)

$view=string    name of the view to be rendered. See getViewFile for details about how the view script is resolved.
$data=array data to be extracted into PHP variables and made available to the view script
$return=boolean whether the rendering result should be returned instead of being displayed to end users
$processOutput=boolean  whether the rendering result should be postprocessed using processOutput.
{return}    string  the rendering result. Null if the rendering result is not required.

修改 上述方案适用于ajax调用renderPArtials。您应该尝试在控制器操作中呈现ajax请求,如

 $this->renderPartial('renderOnAjax', array(
                'user' => $models,
                            'pages' => $pages,
            'user_count'=>$count
        ),false,true);