繁体   English   中英

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

[英]yii2, pagination doesn`t work after ajax and renderPartial

我正在网站上搜索产品。 第一次加载页面时,您会看到所有产品。 产品块包装在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]);

}

我有一个解决方案:

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']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM