Answer the question
In order to leave comments, you need to log in
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(); ?>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question