D
D
Dmitry Petrik2015-04-07 10:20:48
MySQL
Dmitry Petrik, 2015-04-07 10:20:48

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

2 answer(s)
R
Roman Frank, 2015-04-07
@Akellacom

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

And do not forget about
Next, create another action, which is just responsible for loading materials
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;
    }

And the only thing left to do is to initiate an AJAX request to the page ash when scrolling the page.
Something like that, sorry for the formatting of the code. If anyone knows a more elegant solution, then tell
me P.S This is an example for Yii 2, for the first the principle is the same, but the implementation is different, I think you will understand

V
Vladislav Bushuev, 2015-04-07
@redstar

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 question

Ask a Question

731 491 924 answers to any question