yii2,分页在ajax和renderPartial之后不起作用

时间:2017-10-17 09:46:18

标签: php ajax pagination yii2 pjax

我在网站上搜索产品。第一次加载页面时,您会看到所有产品。 产品块包装在pjax中,分页由小部件LinkPager执行。

通过ajax执行对服务器的搜索请求,并通过renderPartial返回找到的产品块。如果我尝试切换到下一页,搜索结果将重置,您将再次看到所有产品。事实证明,在执行renderPartital后,小部件LinkPager无法正常工作。

视图:

<div class="search-wrap">
<input type="text" class="search-input" id="search" placeholder="Поиск по названию" name="p">
<button type="submit" class="btn filter-search-btn-menu"   id="btns">Поиск</button>
 <script>
 $('#btns').on('click', function(e){
    var search = $('#search').val();
       $.ajax({
        type: 'POST',
        url: "/person/notice",
        data: {  'search': search  },
        cache: false,
        success: function (data) {
              $('#allnotice').html(data);
            });
});
</script>
</div>

<div id="allnotice">
<?php yii\widgets\Pjax::begin();?> 
<div id="up">

<?php   if ($offerings)   foreach($offerings as $one): ?>
<div class="preview-notice-wrap">
<div class="preview-notice-inner">
        <h4><?php echo $one->title; ?></h4>
        <div class="preview-notice">
                <div>
                        <p><?php
                                $price = $one->start_price;
                                echo number_format($price) . ' ₽';
                        ?></p>
                </div>
                <div>
                        <p><?php  
                        $date = $one->created;
                        echo Yii::$app->formatter->asDate($date, 'd MMMM y');
                        ?> </p>

                </div>
        </div>
    </div>
</div>
<?php endforeach; ?>
<div class="notice-pagination-wrap preview-notice-pagination">
<?= \app\components\MyPager::widget([
    'pagination' => $pages,
    'maxButtonCount' =>7,
    'prevPageLabel' => false,
    'nextPageLabel' => false,
    'activePageCssClass' => ['class' => 'page-active'],
    'options' => ['class' => 'page-test'],
    'linkOptions' => ['class' => 'page-link'],



    ]); ?>


</div> 

</div>
<?php yii\widgets\Pjax::end(); ?>
</div>

控制器:

public function actionNotice()
    {
        $user_id = Yii::$app->user->id;
        $user = Person::findOne($user_id); 
        if (Yii::$app->request->isPost && Yii::$app->request->isAjax && Yii::$app->request->post()){
        $s = \Yii::$app->request->post('search');

        $count = \app\models\Offering::find()->where(['person_id' => $user->id])->andWhere(['like', 'title', $s])->count(); 

        $query = \app\models\Offering::find()->where(['person_id' => $user->id])->andWhere(['like', 'title', $s])->orderBy('id DESC');
        $pages = new \yii\data\Pagination(['totalCount' => $query->count(),'pageSize' => 4, 'pageParam' => 'page-top']);
        $offerings = $query->offset($pages->offset)->limit($pages->limit)->all();
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         return $this->renderPartial('_notice, ['count' => $count, 'pages' => $pages, 'offerings' => $offerings]);
   }


       $count = \app\models\Offering::find()->where(['person_id' => $user->id])->count();
       $query = \app\models\Offering::find()->where(['person_id' => $user->id])->orderBy('id DESC'); 
       $pages = new \yii\data\Pagination(['totalCount' => $query->count(),'pageSize' => 4, 'pageParam' => 'page-top']);
       $offerings = $query->offset($pages->offset)->limit($pages->limit)->all();

       return $this->render('notice', ['count' => $count, 'pages' => $pages, 'offerings' => $offerings]);

}

1 个答案:

答案 0 :(得分:0)

我有一个解决方案:

url: "/person/notice?search="+search,

将值放入网址。

 $('#btns').on('click', function(e){
    var search = $('#search').val();
       $.ajax({
        type: 'POST',
        url: "/person/notice?search="+search,
        data: {  'search': search  },
        cache: false,
        success: function (data) {
              $('#allnotice').html(data);
            });
});

你可以在控制器中捕获它

    if(isset($_POST['search']))
        $search_word = trim($_POST['search']);
    if(isset($_GET['search']))
        $search_word = trim($_GET['search']);