Pjax keeps refreshing whole page in Yii2, Pjax is called multiple times inside listview

I have been trying to resolve this issue in these few days, basically i am trying to have commetn and child comments, the comment has a button/link called ‘retrievecomment’ to retrieve child comment.

I found out that The pjax is not working, it keeps refreshing the whole page


    <div class="row">   

            <?= $model['comment']?>

    </div>

    <div class="row">

        <div id="selector"align="right">

            <?= Html::a('Retrieve Comment', ["../../thread/index?id=" . $model['thread_id'] . "&comment_id=" . $comment_id], 

                                        ['data-pjax' => '#childCommentData-'.$comment_id, 'class' => 'btn btn-default'

                                        ,'id' => 'retrieveComment' . $comment_id]) ?>

        </div>


        <?php Pjax::begin([

            'id' => 'childCommentData-'.$comment_id,

            'clientOptions'=>[

                    'timeout' => 5000,

                    'registerClientScript' => "$.pjax.reload({container:'#childCommentData-$comment_id'});",

                    'linkSelector'=>'#retrieveComment'.$comment_id

                    ]

            ]);?>


            <div  class="col-md-12">


                <?php if(isset($retrieveChildData)){ ?>

                    <?= ListView::widget([


                            'dataProvider' => $retrieveChildData,

                            'options' => [

                                'tag' => 'div',

                                'class' => 'list-wrapper',

                                    'id' => 'list-wrapper',

                            ],

                                'layout' => "\n{items}\n{pager}",


                            'itemView' => function ($model, $key, $index, $widget) {

                                return $this->render('_list_child_comment',['model' => $model]);

                            }, 

                            'pager' => [

                                'firstPageLabel' => 'first',

                                'lastPageLabel' => 'last',

                                'nextPageLabel' => 'next',

                                'prevPageLabel' => 'previous',

                                'maxButtonCount' => 3,

                            ],

                        ]) ?>


                <?php } ?>




            </div>

        <?php Pjax::end();?>

    </div>


</div>

The Pjax for the comment is called multiple times inside the listview , i believe this has become the problem since the compiler confused which pjax to be reloaded,

Controller code (inside actionIndex)




           else if(!empty($_GET['comment_id'])){

                $comment_id = $_GET['comment_id'];


                //retrieve yes data

                $retrieveChildData = new SqlDataProvider([

                    'sql' => Comment::retrieveChildComment($comment_id),  

                    'totalCount' => Comment::countChildComment($comment_id),

                    'pagination' => [

                        'pageSize' =>5,

                        ],


                ]);

                return $this->render('_list_comment.php', ['retrieveChildData' => $retrieveChildData]);


            }

Try moving timeout above clientOptions




        <?php Pjax::begin([

            'id' => 'childCommentData-'.$comment_id,

            'timeout' => 5000,

            'clientOptions'=>[

                    'registerClientScript' => "$.pjax.reload({container:'#childCommentData-$comment_id'});",

                    'linkSelector'=>'#retrieveComment'.$comment_id

                    ]

            ]);?>



it is still not working, it only take 300 ms while the default is more than 500 ms, i dont think that is the problem

Try "async: false" in your code




$.pjax.reload({

				type: "GET",

				container:"#container",

				data: $("form").serialize(),

				async: false

			});