Answer the question
In order to leave comments, you need to log in
PJAX in Yii2 and field focus?
Good day. Please help me overcome the problem. There is a code:
<?php echo Html::beginForm(); ?>
<div class = "form-horizontal" role = "form" id = "form_message">
<div class = "input-group">
<?php echo Html::activeTextInput($form_model, 'message', ['class' => 'form-control', 'value'=>'', 'autofocus'=>true]); ?>
<?php echo Html::activeHiddenInput($form_model, 'group_id', ['value'=>$group_model->group_id]); ?>
<?php echo Html::activeHiddenInput($form_model, 'to_user', ['value'=>Yii::$app->user->id == $group_model->user_to ? $group_model->user_from : $group_model->user_to]); ?>
<span class="input-group-btn">
<?php echo Html::submitButton('Отправить', ['class' => 'btn btn-default']); ?>
</span>
</div>
</div>
<?php echo Html::endForm(); ?>
<?php echo Html::error($form_model, 'message'); ?>
<?php
yii\widgets\Pjax::begin(['options' => ['id'=>123]]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_message',
'layout' => "{items}\n{pager}"
]);
yii\widgets\Pjax::end();
?>
function updateMessage(){
$.pjax.reload({container: '#123'});
}
$(document).ready(function(){
setInterval(updateMessage, 3000);
});
Answer the question
In order to leave comments, you need to log in
id=123 attribute cannot be set in HTML JS will not find this element use for example 'a123'
ps focusing after updating part of the page can be solved using events
$js=<<<JSCODE
$('#a123).on('pjax:complete', function() {
// ваш код
});
JSCODE;
?>
<?php $this->registerJS($js);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question