javascript - 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">×</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(); ?>
我在不同的页面上使用相同的模式,它似乎在那里工作正常,但我无法发现两个视图页面之间的任何差异。
有谁能帮忙吗?
解决方案
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
推荐阅读
- c - strsafe.h 错误:“,”预期(得到“dwFlags”)
- r - 调用 R 内置函数的 UI
- javascript - 可以在不违反`script-src-attr: 'none' 的情况下使用超HTML 内联式事件处理程序吗?
- jenkins - Jenkins 角色策略插件,如果不在适当的组中,则限制用户构建 - Jenkins 文件
- prolog - 如何指定 ciao 语言模式进行编译
- python - 如何使用 Plotly 制作两个下拉菜单来控制子图
- sql - 在 Synapse 中使用 openrowser(BULK) 的动态存储过程
- sql - 为什么插入命令给出关于新运算符的错误?
- html - 如何使用 css 将类分配给 html 图像?
- c - 无法在内核树之外编译 bindsnoop eBPF c 代码