首页 > 解决方案 > Yii2 Pjax 重新加载整个页面

问题描述

有人可以帮助我解决以下问题吗?我使用模态/表单在屏幕上编辑模型,并通过 Pjax 更新模型。保存没问题,但现在gridview也必须用Pjax更新。

对于我的模态,我使用以下代码

<?php 
$form = ActiveForm::begin(
    ['enableClientValidation' => true, 'options' => ['id' => $model->formName()]]);
?>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title"><?= $actionTitle ?> <?= StringHelper::basename(get_class($model)); ?></h4>
</div>
<div class="modal-body">
    ...
</div>
<div class="modal-footer">
    <?php echo Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

<?php ActiveForm::end();

$this->registerJs(<<<JS
$(document).on('hidden.bs.modal', function (e) {
        $(e.target).removeData('bs.modal');
    });
JS
);

?>

<?php $script = <<< JS

$('form#{$model->formName()}').on('beforeSubmit', function(e){
    var \$form = $(this);
    $.post(
        \$form.attr("action"), //Serialize Yii2 Form
        \$form.serialize()
    ).done(function(result){
        if(result == true){
            $(\$form).trigger("reset");
            $(document).find('#modalphysicalcomponent').modal('hide');
            $.pjax.defaults.timeout = false;
            $.pjax.reload({container:'#Grid'});
        }else{
            $("#message").html(result.message);
        }
    }).fail(function(){
        console.log("server error");
    });

    return false;

});

JS;
$this->registerJs($script);
?>

它应该只更新以下网格视图,而是重新加载整个页面,这会导致错误,因为某些 POST 变量不会重新发送。

<?php Pjax::begin(['id' => 'Grid']); ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                'id',
                'component.tag',
                'component.name',
                'component.parent.tag',
                [
                    'label' => 'Configure',
                    'format'=>'raw',
                    'value' => function ($model, $key, $index, $column){
                        return Html::a('Configure '.get_class($model), ['update-modal', 'id' => $model->component->id], ['data-toggle'=>'modal', 'data-target'=>'#modalphysicalcomponent' ,'class' => 'btn btn-xs btn-primary', ]);
                    }
                ],
            ],
        ]); ?>
<?php Pjax::end(); ?>

我在不同的页面上使用相同的模式,它似乎在那里工作正常,但我无法发现两个视图页面之间的任何差异。

有谁能帮忙吗?

标签: javascriptphpyii2pjax

解决方案


You need to pass the grid id as text, and not object when reloading the gridview. The object is passed as the second parameter which are the options.

This is your mistake

$.pjax.reload({container:'#Grid'});`

it should be like below

$.pjax.reload('#Grid');

See the docs for the Pjax here


推荐阅读