Answer the question
In order to leave comments, you need to log in
How to do ajax page pagination?
I wanted to do the usual ajax pagination, where you click on the show more button and the following data is shown from the bottom, the analog is something like this https://ruseller.com/lessons/les2017/demo/index.html
Only the first one I did not understand how to implement and where can I get data in ajax, that is, make a request to the database again or fix it somehow from the data?
My
view implementation
<div class="comment-order">
<?php foreach ($comments as $com){
switch ($com->user_id){
case Yii::$app->user->id;
$user = 'Я';
break;
case (User::USER_DISAYNER);
$user = 'Дизайнер';
break;
case (User::USER_MASTER):
$user = 'Мастер';
break;
}
echo '
<div style="display: block;">
<div class="userCommit">'.$user.':</div>
<div class="comment">'.$com->comment.'</div>
<div class="dateCommit">'.date('d.m H:i', strtotime($com->date)).'</div>
</div>';
} ?>
<div class="pagePagination" data-id="<?= $model->order_id ?>">Показать еще</div>
</div>
$comments = Comment::find()->where(['id_zakaz' => $model->id_zakaz]);
$pages = new \yii\data\Pagination((['totalCount' => $comments->count(), 'pageSize' => 3]));
$comments = $comments->offset($pages->offset)
->limit($pages->limit)
->all();
$('body').on('click', '.pagePagination', function () {
let id = $(this).data('id');
$.get(`${window.location.origin}/comment/comment-zakaz?id=${id}`)
.done(res => console.log(res))
.fail(err => console.error(err.responseText));
});
public function actionCommentZakaz($id)
{
$comments = Comment::find()->select(['id_user', 'date', 'comment'])->where(['id_zakaz' => $id]);
$pages = new Pagination(['totalCount' => $comments->count(), 'pageSize' => 6]);
$comments = $comments->offset($pages->offset)
->limit($pages->limit)
->all();
return json_encode($comments);
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
As I understand it, you need to make an endless loading of content on the page.
Here is an article written for yii1, but I'm sure you can convert it to yii2.
At least you understand the principle.
I implemented pagination in the following, simple way:
when displaying records on the page in the data-attributes of the button for loading new records, I wrote down such parameters as the id of the last record in the list, the record limit (aka offset). So the button looks like this
When the button is pressed, all these parameters are transmitted by Ajax to the server where the usual SQL is executed, which selects the next block of records for me and returns the template (or JSON) to the client, which builds the desired view from it. In general, pagination is not something complicated, just the same request with a changing OFFSET parameter
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question