D
D
Dmitry Machavariani2014-12-25 16:34:06
Yii
Dmitry Machavariani, 2014-12-25 16:34:06

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();
        ?>

And
function updateMessage(){
        $.pjax.reload({container: '#123'});
    }

    $(document).ready(function(){
        setInterval(updateMessage, 3000);
    });

But the trouble is that I thought that PJAX only updates a certain piece, and everything else remains untouched, but the focus of the form is lost. Please tell me how to make the ListView updated, everything else remains untouched (including the focus of the form). After a successful update, I tried to focus using JS, but the trouble is that the site is also designed for a mobile phone, and I think the user will not really like the ever-appearing keyboard.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2014-12-26
@sanchezzzhak

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 question

Ask a Question

731 491 924 answers to any question