我有一个实体结构:
Deal --> DealCondition <-- Product
id id id
dealConditons product name
我有管理部门:
DealAdmin:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('dealConditions',
'sonata_type_collection',
[
'by_reference' => false,
'required' => false,
],
[
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
]);
...}
ProductConditionAdmin:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('product','sonata_type_model');
}
ProductAdmin:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('name', null);
}
在ProductAdmin页面上,我需要一些javascript来实现我的目的,所以我这样做了:
application.admin.product:
class: ...\ProductAdmin
tags:
- { name: sonata.admin, manager_type: orm, label_translator_strategy: "sonata.admin.label.strategy.underscore"}
arguments:
- ~
- ...\Product
- ~
calls:
- [setTemplates, [{
edit: ApplicationDealBundle:ProductAdmin:edit.html.twig}]]
在edit.html.twig中:
{% block javascripts %}
{{ parent() }}<script type="text/javascript">MyScript();</script>
{% endblock %}
当我打开ProductAdmin作为主页时,一切正常,但当我点击嵌套表格DealConditions上的产品字段旁边的“添加新产品”按钮时,该脚本不存在。 How it looks
如何添加脚本? 感谢。
答案 0 :(得分:0)
您现在可能已经解决了这个问题,但是昨天我也遇到了同样的问题,因此如果其他人来了,我会做些什么。
可能会有更好/更优雅的解决方案,但这暂时还可以。我要做的是覆盖edit_many_script.html.twig
manages the many-to-[one|many] association field popup
从vendor
复制粘贴文件,并将其放在您的app/Resources/SonataAdminBundle/views/CRUD/Association
目录中(或创建文件)。
您将在文件内找到此ajax调用,以检索您实体的管理表单:
// retrieve the form element from the related admin generator
jQuery.ajax({
url: a.attr('href'),
dataType: 'html',
success: function(html) {
Admin.log('[{{ id }}|field_dialog_form_edit] ajax success', field_dialog_{{ id }});
// populate the popup container
field_dialog_content_{{ id }}.html(html);
field_dialog_title_{{ id }}.html("{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}");
Admin.shared_setup(field_dialog_{{ id }});
// capture the submit event to make an ajax call, ie : POST data to the
// related create admin
jQuery(document).on('click','#field_dialog_{{ id }} a', field_dialog_form_action_{{ id }});
jQuery('form', field_dialog_{{ id }}).on('submit', field_dialog_form_action_{{ id }});
// open the dialog in modal mode
field_dialog_{{ id }}.modal();
Admin.setup_list_modal(field_dialog_{{ id }});
}
});
您可以在此处添加脚本。请注意,默认情况下,它会用于所有X关系,因此,如果只想在特定情况下加载脚本,则可以像下面这样测试{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}
:
// retrieve the form element from the related admin generator
jQuery.ajax({
url: a.attr('href'),
dataType: 'html',
success: function(html) {
Admin.log('[{{ id }}|field_dialog_form_edit] ajax success', field_dialog_{{ id }});
// populate the popup container
field_dialog_content_{{ id }}.html(html);
field_dialog_title_{{ id }}.html("{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}");
Admin.shared_setup(field_dialog_{{ id }});
// capture the submit event to make an ajax call, ie : POST data to the
// related create admin
jQuery(document).on('click','#field_dialog_{{ id }} a', field_dialog_form_action_{{ id }});
jQuery('form', field_dialog_{{ id }}).on('submit', field_dialog_form_action_{{ id }});
// Custom JS for PopUpWidget Entity
if("{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}" === "PopUpWidget"){
//your script
}
// open the dialog in modal mode
field_dialog_{{ id }}.modal();
Admin.setup_list_modal(field_dialog_{{ id }});
}
});