【问题标题】:symfony collection type add javascript functions to collection fieldssymfony 集合类型将 javascript 函数添加到集合字段
【发布时间】:2019-08-30 19:52:31
【问题描述】:

我在我的收藏字段中添加了一些 javascript。 但是我不知道如何在不加倍的情况下以一种好的方式编写 javascript,所以每个新的或退出的字段都包含这个 javascript。

提前致谢:)

我现在用这个:

$('a').on('click', function() {
        setTimeout( function () {
        $('#property_propertydistances_0_icon').fontIconPicker({
            source:    ['icon-heart', 'icon-search', 'icon-user', 'icon-tag', 'icomoon-home2'],
            emptyIcon: false,
            hasSearch: false
        });
            } , 300 );
});

    jQuery(document).ready(function($) {
        $('#property_propertydistances_0_icon').fontIconPicker({
            source:    ['icon-heart', 'icon-search', 'icon-user', 'icon-tag', 'icon-help'],
            emptyIcon: false,
            hasSearch: false
        });
    });

$('a').on('click', function() {
        setTimeout( function () {
        $('#property_propertydistances_1_icon').fontIconPicker({
            source:    ['icon-heart', 'icon-search', 'icon-user', 'icon-tag', 'icomoon-home2'],
            emptyIcon: false,
            hasSearch: false
        });
            } , 300 );
});

    jQuery(document).ready(function($) {
        $('#property_propertydistances_1_icon').fontIconPicker({
            source: fnt_icons_2,
            theme: 'fip-darkgrey'
        });
    });

我使用easyadmin 来调整字段,只能用formbuilder 和js 代码来完成。

公共函数 buildForm(FormBuilderInterface $builder, array $options)

{

$builder

    ->add('icon', TextType::class, array('label' => 'Icon', 'empty_data' => 'icon','label_attr' => array('style'=> '') ))

    ->add('title', TextType::class, array('label' => 'Title (English)', 'empty_data' => 'name','label_attr' => array('style'=> '') ))

    ->add('title_th', TextType::class, array('label' => 'Title (Thai)', 'empty_data' => 'object','label_attr' => array('style'=> '') ))

    ->add('distance', NumberType::class, array('label' => 'Distance (km)', 'empty_data' => '4','label_attr' => array('class'=> 'col-4') ))


;

}

然后我只加载 js en css 文件,文本文件被该 jquery 函数覆盖。我用https://fonticonpicker.github.io/

这是我的 easyadminyaml 代码部分

                - { property: 'propertydistances', css_class: 'propertydistancejava distance-collectionstyling',  id: 'testid1', type: 'collection', type_options: { entry_type: 'App\Form\DistanceType', by_reference: false,  attr: { name: 'testname2', id: 'testid2'} }}      

【问题讨论】:

  • 不使用 id ('#property_propertydistances_1_icon') 并使用类?
  • 或者使用所有以$('[id^="property_propertydistancest_"]')开头的id
  • 感谢您的反馈。你能更具体一点吗?我不擅长 javascript 函数、循环等。
  • 你能告诉我们twig中的负责部分和生成的html代码吗?
  • 我更新了我的帖子。我使用覆盖输入文本字段的 jquery 插件

标签: jquery symfony symfony4 easyadmin


【解决方案1】:

在您的 $builder 中,将类添加到您的字段。然后,在您的 javascript 中,迭代您的类以添加 JavaScript 代码。例如:

    $builder
        ->add('distance', TextType::class, [
            'attr' => ['class' => 'property-js'],
        ])
        ->add('image', TextType::class, [
            'attr' => ['class' => 'property-js'],
        ])

    jQuery(document).ready(function($) {
        $('.property-js').each(function(elem) {
            elem.fontIconPicker({
               source:    ['icon-heart', 'icon-search', 'icon-user', 'icon-tag', 'icon-help'],
               emptyIcon: false,
               hasSearch: false
            });
        });
    });

【讨论】:

  • 感谢您的反馈。我尝试实现 elem。把我得到一个错误我稍微改变了你的代码,现在有 $this 并且这工作 finr codejQuery(document).ready(function($) { $('.property-js').each(function( $this) { $(this).fontIconPicker({ source: pack1, emptyIcon: false, hasSearch: false }); }); });code
猜你喜欢
  • 2019-03-05
  • 2018-10-24
  • 1970-01-01
  • 2015-10-08
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多