简体   繁体   English

Symfony2 - Sonata Admin - 在字段之前添加javascript

[英]Symfony2 - Sonata Admin - add javascript before field

In admin class: 在管理类中:

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('name', 'text')
            ->add('description', 'text')
    ;
}

I don't know how I can before "name" add javascript, can you help me? 我不知道在“名字”添加javascript之前我怎么能,你能帮助我吗?

Working for me: 为我工作:

In admin class src\\PP\\TestBundle\\TestAdmin.php 在管理类src \\ PP \\ TestBundle \\ TestAdmin.php中

public function configure() {
    $this->setTemplate('edit', 'PPTestBundle:CRUD:edit_javascript.html.twig');
}

In src\\PP\\TestBundle\\Resources\\views\\edit_javascript.html.twig src \\ PP \\ TestBundle \\ Resources \\ views \\ edit_javascript.html.twig中

{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('bundles/pptest/admin/js/myscripts.js') }}" type="text/javascript"></script>
{% endblock %}

When you do all this stuff and you have upload myscripts.js you should send this in command line: 当你完成所有这些工作并且你已经上传了myscripts.js时,你应该在命令行中发送它:

app/console assets:install web

(possible that I forgot something) (可能我忘记了什么)

Sorry for my bad English :<> 抱歉我的英文不好:<>

EDITED EDITED

You need to create a custom TWIG template for it (where you could place your javascript code just before the widget code). 您需要为它创建一个自定义TWIG模板(您可以在窗口小部件代码之前放置您的javascript代码)。

Then you write inside ap/config/config.yml where your custom template is to allow Symfony and SonataAdmin to recognize it. 然后你在ap/config/config.yml里写,你的自定义模板允许Symfony和SonataAdmin识别它。

You have some info here Sonata Admin - Custom template 你在这里有一些信息索纳塔管理员 - 自定义模板

More info here customize field types 更多信息在这里自定义字段类型

An example could be something like this: 一个例子可能是这样的:

Admin class 管理员班

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('name', 'ajax_autocomplete')
            ->add('description', 'text')
    ;
}

And, in the TWIG template you need to extend from the Sonata Admin field template that better fits your necessities. 而且,在TWIG模板中,您需要从更适合您的必需品的Sonata Admin字段模板进行扩展。 In this case maybe base_edit.html.twig or edit_text.html.twig 在这种情况下,可能是base_edit.html.twigedit_text.html.twig

You have a list of templates to extend from, inside this Sonata Admin dir: vendor\\sonata-project\\admin-bundle\\Sonata\\AdminBundle\\Resources\\views\\CRUD 你有一个模板列表,可以在这个Sonata Admin目录中扩展: vendor\\sonata-project\\admin-bundle\\Sonata\\AdminBundle\\Resources\\views\\CRUD

Customization 定制

Imagine you have placed your custom template inside XXXBundle:YYY:ajax_autocomplete.html.twig 想象一下,您已将自定义模板放在XXXBundle:YYY:ajax_autocomplete.html.twig

I think it should work if you write a line here: 我认为如果你在这里写一行,它应该工作:

sonata_doctrine_orm_admin:
    templates:
        types:
            list:
                ajax_autocomplete: XXXBundle:YYY:ajax_autocomplete.html.twig

Starting from sonata admin 3.x you can add/remove js/css to/from the page without extending it. 从sonata admin 3.x开始,您可以在不扩展页面的情况下向页面添加/删除js / css。

sonata_admin:
    ....
    assets:
        # javascript paths to add to the page in addition to the list above
        extra_javascripts:
            - 'your js file path'
        # javascript paths to remove from the page
        remove_javascripts: 
            - 'your js file path'

you can find more information here https://github.com/sonata-project/SonataAdminBundle/pull/4836/files?short_path=e252be0#diff-e252be027e26148c11d971dc969f4be0 你可以在这里找到更多信息https://github.com/sonata-project/SonataAdminBundle/pull/4836/files?short_path=e252be0#diff-e252be027e26148c11d971dc969f4be0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM