0

在将相关记录链接到主记录后,我尝试能够从服务器获取一些数据透视数据的值。这是输入数据透视数据的步骤,其中一些应该会生成(手动输入没有问题,问题是通过AJAX进行):

相关数据

下面是一个相关fields.yaml的例子

fields:
    pivot[sort]:
        label: Order
        type: number
        span: auto
    pivot[time_from_start_to_sight]:
        label: Time from start to the sight
        type: number
        span: auto
    pivot[add_route_time]:
        label: Add time
        type: number
        span: auto
    pivot[add_route_price]:
        label: Basic add. price
        type: number
        span: auto
    pivot[stay_time_default]:
        label: Default stay time
        type: number
        span: auto
    pivot[stay_time_min]:
        label: Min. stay time
        type: number
        span: auto
    pivot[stay_price]:
        label: Price per quarter of hour
        type: number
        span: auto
    _gmaps_sync:
        label: You may try to get data from Google Maps API
        type: gmapsroutesightsync
        commentAbove: Make sure you have entered Google Maps API key

如您所见,我添加了一个自定义小部件来绘制按钮。我什至可以调用小部件的 AJAX 处理程序并将新值应用于传递的数据,并将它们保存到控制器内的 DB。

但我一点也不知道如何推送更新的部分或用新值更新部分。当然,可以通过无组织的 JS 代码更新每个字段 - 但我相信有一个聪明的方法。

请指出最佳实践。谢谢你。

4

1 回答 1

1

可能您需要更新部分表单,从控制器推送它,您将在其中更新所有数据并保存它。

我猜对于数据透视表,您已经定义了将呈现“关系”的部分,因此您可以更新该部分。

当您使用十月的 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">&times;</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">&times;</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 ?>

我认为它应该做你的工作。如果它不起作用,那么我需要你的代码;)

于 2017-12-11T09:06:50.090 回答