可能您需要更新部分表单,从控制器推送它,您将在其中更新所有数据并保存它。
我猜对于数据透视表,您已经定义了将呈现“关系”的部分,因此您可以更新该部分。
当您使用十月的 ajax 框架进行 ajax 调用时,您可以使用类似的东西
data-request-update="relation: '#Form-relationClassesManagePivotForm'
或者
如果您以编程方式发出请求,您可以这样做
$.request('onRefreshTime', {
update: { relation: '#Form-relationClassesManagePivotForm' }
})
您可以在此处详细检查 api:https ://octobercms.com/docs/ajax/update-partials
在这里,我的枢轴关系名称是“类”,因此 id 是这样生成的,但是您可以检查元素以获取正确的 id,它将位于“modal-body”内。
现在从控制器你只需要推送更新的部分,它将自动更新。
function onRefreshTime()
{
return [
'#Form-relationClassesManagePivotForm' => $this->renderPartial('pivot_form')
// or 'relation' => $this->renderPartial('pivot_form')
];
}
在这里最好在渲染页面的同一控制器上编写此方法,因为它已经定义了关系的定义,因此关系小部件已经构建。
如果有任何困惑,请在评论中告诉我。
更新
您需要在更新字段的控制器中添加此代码
return ['#myform' => $this->asExtension('RelationController')->onRelationClickManageListPivot()];
并且您需要从关系管理器中添加一个部分覆盖。
你可以看到我正在添加 custm id = 'id'=>"myform"
_relation_pivot_form.htm(从关系管理器复制并粘贴到您的控制器文件夹中)
<?php if ($relationManageId): ?>
<?= Form::ajax('onRelationManagePivotUpdate', [
'data' => ['_relation_field' => $relationField, 'manage_id' => $relationManageId],
'data-popup-load-indicator' => true,
'id'=>"myform"
]) ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="popup">×</button>
<h4 class="modal-title"><?= e(trans('backend::lang.relation.related_data', ['name'=>trans($relationLabel)])) ?></h4>
</div>
<div class="modal-body">
<?= $relationPivotWidget->render(['preview' => $this->readOnly]) ?>
<button
type="button"
data-request="onPivotRefresh"
class="btn btn-primary">
Refresh Partial
</button>
</div>
<div class="modal-footer">
<?php if ($this->readOnly): ?>
<button
type="button"
class="btn btn-default"
data-dismiss="popup">
<?= e(trans('backend::lang.relation.close')) ?>
</button>
<?php else: ?>
<button
type="submit"
class="btn btn-primary">
<?= e(trans('backend::lang.relation.update')) ?>
</button>
<button
type="button"
class="btn btn-default"
data-dismiss="popup">
<?= e(trans('backend::lang.relation.cancel')) ?>
</button>
<?php endif ?>
</div>
<?= Form::close() ?>
<?php else: ?>
<?= Form::ajax('onRelationManagePivotCreate', [
'data' => ['_relation_field' => $relationField, 'foreign_id' => $foreignId],
'data-popup-load-indicator' => true
]) ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="popup">×</button>
<h4 class="modal-title"><?= e(trans('backend::lang.relation.related_data', ['name'=>trans($relationLabel)])) ?></h4>
</div>
<div class="modal-body">
<?= $relationPivotWidget->render() ?>
</div>
<div class="modal-footer">
<button
type="submit"
class="btn btn-primary">
<?= e(trans('backend::lang.relation.add')) ?>
</button>
<button
type="button"
class="btn btn-default"
data-dismiss="popup">
<?= e(trans('backend::lang.relation.cancel')) ?>
</button>
</div>
<?= Form::close() ?>
<?php endif ?>
我认为它应该做你的工作。如果它不起作用,那么我需要你的代码;)