我正在为 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(),
));
}