A
A
Andrey2018-04-05 17:16:17
JavaScript
Andrey, 2018-04-05 17:16:17

How to update 2 pjax objects with one request?

I have two grid tables on one page, each wrapped in its own PJAX with its own id. It is necessary that there would be an auto-update every n seconds. But can't get it to work right

<?php Pjax::begin([
                'id' => 'select_pjax'
            ])?>
            <?= GridView::widget([
                'dataProvider' => $dataProviderTemp,
                'columns' => [
                    ......]])
<?php
            $this->registerJs(
                '$("document").ready(function(){
                            setTimeout(function(){
                                $.pjax.reload({container:"#select_pjax"});  //Reload GridView
                            },5000);
                    });'
            );?>
<?php Pjax::end()?>
код всякий разный
<?php Pjax::begin([
                'id' => 'save_pjax'
            ])?>
            <?= GridView::widget([
                'dataProvider' => $dataProviderTemp,
                'columns' => [
                    ......]])
<?php
            $this->registerJs(
                '$("document").ready(function(){
                            setTimeout(function(){
                              $.pjax.reload("#save_pjax");  //Reload GridView
                            },4000);
                    });'
            );?>
<?php Pjax::end()?>

At some point, overlap occurs and only one request is processed. How can I make sure both are updated?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Romanovich, 2018-09-20
@DronTat

asynchronously they need to be executed
$.pjax.reload({ container: #id, async:false });

M
Maxim Timofeev, 2018-04-05
@webinar

https://github.com/yiisoft/jquery-pjax
Call in js to update containers by their id, for example:

$.pjax.reload('#pjax-container', options);
$.pjax.reload('#pjax-container2', options);

You can hook on the event of the first container and update the second one.

S
sokollondon, 2019-08-05
@sokollondon

Option 1. Use normal ajax

$.ajax({
    'success': function(data){
        var targets = [
            '#container1',
            '#container2'
        ];
        $.each(targets, function (ind, t) {
            $(t).html($(data).find(t).html());
        });
    }
});

Option 2: Use pjax but there will be 2 requests
$.pjax.reload({container: "#container1", async:false});
$.pjax.reload({container: "#container2", async:false});

PS Pjax developers have been contacted since 2012 with a request to make 1 request, they do not plan to do so

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question