D
D
DivineMgn2015-12-09 22:32:29
Yii
DivineMgn, 2015-12-09 22:32:29

How do I set the GridView to refresh periodically?

I would like to periodically update individual fields (pull data from the database) GridView after a certain period without reloading the entire page.
At the moment I have this implementation:
View :

<?php
$script = <<< JS
$(module).ready(function() {
    setInterval(function() {     
      $.pjax.reload({container:'#state-dependence'});
    }, 5000); 
});
JS;
$this->registerJs($script);
?>

....
<?php Pjax::begin(); ?>
    <?= GridView::widget([ 'id'=>'module',
        'dataProvider' => $dataProvider,
        'layout' => '{items}',
        'tableOptions' => ['class' => 'table table-striped table-bordered'],
        'columns' => [
             'Id', 
             'Title',
             [ 
               'format' => 'raw',
               'value' => function ($model, $key, $index, $column) {
                return Html::tag('span', Html::encode($model::GetStringState($model->State)), 
                                ['id' => 'state-dependence']);
               }
             ],
             [ 
               'format' => 'raw',
               'value' => function ($model, $key, $index, $column) {
                return Html::tag('span', Html::encode($model->Value), 
                                ['id' => 'state-dependence']);
               }
             ]
             
        ],
    ]); ?>
<?php Pjax::end(); ?>

But in this implementation, a full page refresh occurs. Is there a better implementation of periodic content refresh?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Programep, 2015-12-10
@Programep

Add unique id to fields (row2_col12_tbname), for example.
Do it via ajax function:

setInterval(function() {
$.ajax({
url: "script.php",
method: "POST",
data: { id : menuId },
dataType: "html",
done: function( msg ) {
$( "#log" ).html( msg );
},
fail: function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
}
});
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question