1

我正在为 Symfony2 使用 Easy Admin Bundle。如何将我自己的模式对话框与文本输入一起用于列表中的自定义操作?

假设自定义操作称为重命名,然后在列表中点击按钮重命名后,我想在模式对话框中写一个新名称。然后点击 OK 按钮后,我想在控制器中调用 ranameAction 来完成所需的一切,但必须将名为 name 的参数发送到那里。

我有解决方案,但它并不好,必须有一些更好的方法。

我创建了 _simple_form.html.twig:

{{
    form(rename_form, {
        action: rename_form.vars.action ~ '&referer=' ~ referer,
        method: 'POST',
        attr: { id: 'rename-form', style: 'display: none' }
    })
}}

<div id="modal-rename" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <h4>{{ 'rename_modal.title'|trans(_trans_parameters, 'EasyAdminBundle') }}</h4>
                <p>{{ 'rename_modal.content'|trans(_trans_parameters, 'EasyAdminBundle') }}</p>
                <br />
                <input class="form-control" type="text" />
            </div>
            <div class="modal-footer">
                <button type="button" data-dismiss="modal" class="btn">
                    {{ 'action.cancel'|trans(_trans_parameters, _translation_domain) }}
                </button>

                {% if easyadmin_action_is_enabled(view, 'rename', _entity_config.name) %}
                    {% set _rename_action = easyadmin_get_action(view, 'rename', _entity_config.name) %}
                    <button type="button" data-dismiss="modal" class="btn btn-danger" id="modal-rename-button" formtarget="{{ _rename_action.target }}">
                        {% if _rename_action.icon %}<i class="fa fa-{{ _rename_action.icon }}"></i>{% endif %}
                        {{ 'rename_modal.action'|trans(_trans_parameters, 'EasyAdminBundle') }}
                    </button>
                {% endif %}
            </div>
        </div>
    </div>
</div>

在 config.yml 中有一个新选项:

list:
                actions:
                    rename:    { name: rename,    type: method, label: 'Rename', css_class: 'btn btn-primary', icon: pencil }

在我正在使用的 list.html.twig 中:

{% block main %}

{% block rename_form %}
        {% set referer = paginator.currentPage == paginator.nbPages and 1 != paginator.currentPage and 1 == paginator.currentPageResults.count
            ? path('easyadmin', app.request.query|merge({ page: app.request.query.get('page') - 1 }))
            : app.request.requestUri
        %}

        {{ include('@EasyAdmin/default/includes/_simple_form.html.twig', {
            view: 'list',
            referer: referer|url_encode,
            rename_form: rename_form_template,
            _translation_domain: _entity_config.translation_domain,
            _trans_parameters: _trans_parameters,
            _entity_config: _entity_config,
        }, with_context = false) }}
    {% endblock rename_form %}    
{% endblock %}

{% block body_javascript %}
    {{ parent() }}
    <script type="text/javascript">
    $(function() {
        $('#modal-rename-button').on('click', function(e) {
            e.preventDefault();

            var name = $('#name').val();

            var renameForm = $('#rename-form');
                    renameForm.attr('action', renameForm.attr('action').replace('__name__', name));
        });

        $('a.action-rename').on('click', function(e) {
            e.preventDefault();
            var id = $(this).parents('tr').first().data('id');

            $('#modal-rename').modal({ backdrop: true, keyboard: true })
                .off('click', '#modal-rename-button')
                .on('click', '#modal-rename-button', function () {
                    var renameForm = $('#rename-form');
                    renameForm.attr('action', renameForm.attr('action').replace('__id__', id));
                    renameForm.trigger('submit');
                });
        });
    });
</script>
{% endblock %}

最后是控制器:

protected function createRenameForm($entityName, $entityId, $name)
    {
        $formBuilder = $this->get('form.factory')->createNamedBuilder('rename_form')
            ->setAction($this->generateUrl('easyadmin', array('action' => 'rename', 'entity' => $entityName, 'id' => $entityId, 'name' => $name)))
            ->setMethod('POST')
        ;

        $formBuilder->add('submit', LegacyFormHelper::getType('submit'), array('label' => 'rename_modal.action', 'translation_domain' => 'EasyAdminBundle'));
        $formBuilder->add('_easyadmin_rename_flag', LegacyFormHelper::getType('hidden'), array('data' => '1'));

        return $formBuilder->getForm();
    }

    public function listAction() {
        $this->dispatch(EasyAdminEvents::PRE_LIST);

        $fields = $this->entity['list']['fields'];
        $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->config['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']);

        $this->dispatch(EasyAdminEvents::POST_LIST, array('paginator' => $paginator));

        return $this->render($this->entity['templates']['list'], array(
            'title' => 'Branches',
            'entity' => $this->entity['name'],
            'currentBranch' => $this->repo->getCurrentBranchName(),
            'paginator' => $paginator,
            'fields' => $fields,
            'delete_form_template' => $this->createDeleteForm($this->entity['name'], '__id__')->createView(),
            'rename_form_template' => $this->createRenameForm($this->entity['name'], '__id__', '__name__')->createView(),
        ));
    }
4

1 回答 1

0

我有一个类似的问题,因为我想在执行自定义操作之前显示一个模式。所以我所做的是用jquery修改按钮动作

$(document).ready(function () {
    var $archiveButton = $('a.action-archive');

    $archiveButton.attr('href', '#');
    $archiveButton.attr('data-toggle', 'modal');
    $archiveButton.attr('data-target', '#archive-modal');
    $archiveButton.attr('data-url', '/url/to/load/form/inside/modal/body');
});

我不知道这是否是更好的方法,但它帮助我在执行之前向用户显示了一些archiveAction()信息AdminController

已编辑:一种访问 POST 变量的方法

您可以通过 Container 访问 POST 变量

public function renameAction()
    {
        $name = $this->container->get('request')->get('your variable');
        var_dump($name);
        die();
        return $this->redirectToRoute('admin', ['entity' => 'Branch', 'action'     => 'list']);
    }

玩一会儿,$this->container->get('request')找到你要找的东西

于 2017-03-30T20:04:42.680 回答