There is a chat on yii2. In the chat, pjax is used to upload messages and change dialogs. There is a script that scrolls to the end of the message. One pjax widget is nested in another. (id different).

External pjax is updated when a dialog is selected and when a message is sent. Internal pjax is updated every 10 sec.

It looks like this:

... //внешний pjax <?php Pjax::begin([ 'id' => 'all-chat', 'timeout' => 5000, 'formSelector' => '.input_msg_write' ]) ?> ... //некоторый код ... <div class="msg_history"> //блок с сообщениями, который нужно пролистать вниз //внутренний pjax <?php Pjax::begin([ 'id' => 'list-messages', 'timeout' => 5000, 'enablePushState' => false, 'formSelector' => false, 'linkSelector' => false ]) ?> ... //тут сообщения которые обновляются раз в 10 сек ... <?php Pjax::end() ?> </div> ... //еще немного кода ... <?php Pjax::end() ?> ... 

It is necessary that the script that scrolls to the end of the message works only when the external pjax is updated, but does not work when the internal is updated.

I tried to write like this:

 $('#all-chat').on('pjax:success',function () { scrollTopChat(); }) 

but then the messages are scrolled when updating any of the two pjaxes

    1 answer 1

    I decided to check inside the script which container was updated:

     $(document).on('pjax:success',function (event, data, status, xhr, option) { if (option.container.selector == '#all-chat') { scrollTopChat(); } }) 

    So the necessary script is launched after the update of the necessary Pjax container, and not with any update

    • To supplement your question, we ask you to use the edit option. The “Post Reply” button should only be used for comprehensive answers to questions. - from the queue of checks - LFC
    • This answer fully satisfies my question. The problem is solved in such a script, so I think that this is a fairly comprehensive answer to this question. - Genius Jokes