I implement below example for autorefresh of time.(only interval function is set for one sec)
http://blog.neattutorials.com/examples/pjax/web/site/auto-refresh
I use this implementation on my VIEW where i use Inputform and i have trouble that each reload i lost pointer from inputform.
Please is there some way, how Javascript can call function from controller without using button or how i can reload part of page without losing pointer from my text input??
I tried window.location.replace("site/auto-refresh"); but it refresh whole site, and not part of PJAX.
There is my code:
<?php $form = ActiveForm::begin(['id' => 'my-form']);
echo $form->field($model,'ipadress')->textInput((['id'=>'ipadress'])); $model->text = $text;
echo $form->field($model, 'text')->textArea(['rows' => '6','readonly' => 'true']);
echo Html::submitButton('Submit',['class'=>'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
<?php Pjax::begin(); ?>
<?= Html::a("Refresh", ['site/auto-refresh'], ['class' => 'btn btn-lg btn-primary', 'id' => 'refreshButton',]) ?>
<h1>Current time: <?= $time ?></h1>
<?php Pjax::end(); ?>
<?php
$script = <<< JS
$(document).ready(function() {
setInterval(function(){ $("#refressetIntervalhButton").click(); }, 1000);
});
JS;
$this->registerJs($script);
?>
UPDATE: Still the same result, when time is updated i lost pointer from text input(when i writting). VIEW:
<?php $form = ActiveForm::begin(['id' => 'my-form']); ?>
<?php echo $form->field($model,'ipadress')->textInput((['id'=>'ipadress']));
$model->text = $text; ?>
<?= $form->field($model, 'text')->textArea(['rows' => '6','readonly' => 'true']) ?>
<?= Html::submitButton('Submit',['class'=>'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
<?php Pjax::begin(); ?>
<?= Html::a("Refresh", ['site/auto-refresh'], ['class' => 'btn btn-lg btn-primary', 'id' => 'refreshButton',]) ?>
<h1>Current time: <?= $model->time ?></h1>
<?php Pjax::end(); ?>
Controller:
$model = new \app\models\Tools;
$model->time = date('H:i:s');
$uptime=shell_exec('uptime | awk \'{print $3}\';');
return $this->render('auto-refresh', ['uptime' => $uptime, 'text' => $ip,'model' => $model]
Thank you MK
Please use one controller action for both and add time as public property in your model.
Controller
public function actionTest()
{
$model = new Test();
$model->time = date('H:i:s');
return $this->render('TestView', ['model' =>$model]);
}
View
<?php Pjax::begin(); ?>
<?= Html::a("Refresh", ['site/test'], ['class' => 'btn btn-lg btn-primary', 'id' => 'refreshButton',]) ?>
<h1>Current time: <?= $model->time ?></h1>
<?php Pjax::end(); ?>
Or you don't want time as public property your controller return like below
return $this->render('TestView', ['model' => $model,'time'=>date('H:i:s')]);
And use time variable in view directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.