Answer the question
In order to leave comments, you need to log in
How to make pagination inside Pjax after renderPartial?
Hello, I'm doing a simple search for products on the site. When the page is loaded for the first time, it displays a list of all products. Sending a request to the server using ajax
<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>
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]);
}
<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>
Answer the question
In order to leave comments, you need to log in
You have a very wrong approach.
Getting a list of products using a post request is not true, there is a get for this. Post by definition serves to change information, get in turn to receive information.
In your case, you need to use ActiveDataProvider + ListView + Pjax.
The principle of operation is this, while the filter is not active, the entire list of products is displayed, when the filter is activated, the logic for selecting products is written in the same action, the selected products are returned to the same view. Pjax itself will send a request and insert the data in the right place, ActiveDataProvider+ListView this bundle will paginate objects by itself.
<?php yii\widgets\Pjax::begin();?>
// Здесь форма с фильтром
// Здесь ListView
<?php yii\widgets\Pjax::end(); ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question