轻松管理包模式对话框自定义操作?

时间:2017-03-30 15:45:22

标签: php symfony modal-dialog symfony2-easyadmin

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

假设自定义操作被称为重命名,在点击按钮后重命名列表我想在模态对话框中写一个新的名称。然后按下按钮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(),
        ));
    }

1 个答案:

答案 0 :(得分: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之前向用户显示了一些信息

EDITED:一种访问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')玩一会儿,找到您要找的内容