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

它应该只更新下面的 gridview,而是重新加载整个页面,这会导致错误,因为某些 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(); ?>

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

谁能帮忙?

解决方法

您需要在重新加载 gridview 时将网格 ID 作为文本传递,而不是对象。该对象作为选项的第二个参数传递。

这是你的错误

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

应该如下图

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

查看 Pjax 的文档 here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...