сделать пагинацию после renderPartial

Общие вопросы по использованию второй версии фреймворка. Если не знаете как что-то сделать и это про Yii 2, вам сюда.
Ответить
AndyP
Сообщения: 5
Зарегистрирован: 2017.09.20, 18:01

сделать пагинацию после renderPartial

Сообщение AndyP »

Здравствуйте, делаю поиск объявлений на сайте. При первой загрузки страницы на экране все объявления, которые можно пролистать через виджет внутри pjax.
Поисковый запрос на сервер осуществляется через ajax и возвращается блок найденных объявлений, но если в найденных объявлениях постараться переключить страницу, то поисковый запрос сбрасывается и возвращаются все объявления, как это можно исправить, или быть может я совершенно неверно реализую поиск?

контроллер:

Код: Выделить всё

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]);
 
    }
    
Вид notice

Код: Выделить всё

<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>
Вид _notice

Код: Выделить всё

<?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(); ?> 
Ответить