Answer the question
In order to leave comments, you need to log in
Yii2. Loading old old messages via ajax. How?
I'm doing a chat. At the beginning, the user sees the 20 most recent messages. By scrolling the scroll up, 20 more older messages are loaded by ajax. Etc.
This is where the problems started. Pull out the last 20 , and then another 20 following them ... All this is aggravated by the fact that the message ids are out of order (for example: 1,2,3,7,9,15). Standard Yii functions are indispensable here. Maybe there is some elegant solution?
I also remembered pagination. Can through it somehow it is possible to implement the necessary functionality?
Answer the question
In order to leave comments, you need to log in
Yes, you can use pagination.
Make a hidden field with the current page number, as the user starts to scroll, initialize the ajax request, take the page number and add 1 to it, thus getting the data for the next. pages.
Output the result, and update the hidden field with the current page number.
upd.
Well look.
First, create a hidden field (we need it to determine the current page)
Next, in the action you need, initialize pagination, something like:
$query = ... запрос к БД
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$pages->setPageSize(10);
$model = $query->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('index', [
'model' => $model,
'pages' => $pages,
]);
public function actionPage()
{
if(Yii::$app->getRequest()->getIsAjax()):
$query = ...запрос к бд...;
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$pages->setPageSize(10);
$models = $query->offset($pages->offset)->limit($pages->limit)->all();
$max_pages = $countQuery->count() / 10;
if($_POST['current_page'] > ceil($max_pages)) {
return false;
} else {
return $this->renderPartial('_page', [
'model' => $models,
'pages' => $pages,
]);
}
endif;
}
www.elisdn.ru/blog/28/beskonechnaia-lenta-zapisei-...
If desired, you can optimize for Yii2.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question