【问题标题】:Customize CSS in collection type symfony在集合类型 symfony 中自定义 CSS
【发布时间】:2016-04-05 01:31:22
【问题描述】:

我正在使用呈现多个选择输入的 collectionType,我想将 select2 css 类添加到我的表单中,但它不起作用。

这是包含集合的表单。

 ->add('arrayDiagnosticos', 'collection', [
                'type' => 'entity',
                'label' => 'Diagnósticos dinámicos',
                'allow_add' => true,
                'allow_delete' => true,

                'attr' => [
                        'class' => 'select2',
                    ],
                'options' => [
                   'empty_value' => 'Seleccionar Diagnóstico',
                    'class' => 'AppBundle:Diagnostico',
                    'required' => true,
                    'label' => 'Buscador de Diagnósticos',
                    'attr' => [
                        'class' => 'select2',
                    ],
                ],
            ])

树枝是

<ul class="tags" data-prototype="{{  form_widget(form.arrayDiagnosticos.vars.prototype)|e }}">
{# iterate over each existing tag and render its only field: name #}

    {% for diagnostico in form.arrayDiagnosticos %}
    <li>
      {{ form_row(diagnostico.nombreDiagnostico) }}
    </li>
   {% endfor %}

</ul>

它应该像这样呈现一个选择输入: 1

但它呈现像常规选择输入 2

这是输出的html

<div class="form-group"><label class="control-label required">Diagnósticos dinámicos</label>    
<div id="paciente_form_arrayDiagnosticos" class="select2" data-prototype="&lt;div class=&quot;row&quot;&gt;&lt;div class=&quot;col-xs-9&quot;&gt;&lt;select id=&quot;paciente_form_arrayDiagnosticos___name__&quot; name=&quot;paciente_form[arrayDiagnosticos][__name__]&quot; required=&quot;required&quot; class=&quot;select2 form-control&quot;&gt;&lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;Seleccionar Diagnóstico&lt;/option&gt;            &lt;option value=&quot;1&quot; &gt;se siente mal&lt;/option&gt;            &lt;option value=&quot;2&quot; &gt;asfd&lt;/option&gt;            &lt;option value=&quot;3&quot; &gt;YOLO&lt;/option&gt;&lt;/select&gt;&lt;/div&gt;&lt;div class=&quot;col-xs-3&quot;&gt;&lt;a href=&quot;#&quot; class=&quot;btn btn-danger btn-sm&quot; data-removefield=&quot;collection&quot; data-field=&quot;__id__&quot;&gt;Eliminar Diagnóstico&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;" data-prototype-name="__name__"><ul class="bc-collection list-unstyled"></ul><a href="#" class="btn btn-primary btn-sm" data-addfield="collection" data-collection="paciente_form_arrayDiagnosticos" data-prototype-name="__name__">Agregar Diagnóstico</a></div></div>

我也尝试通过 jQuery 来实现,但没有成功

<script>
$(function(){
    $('#paciente_form_arrayDiagnosticos').addClass('select2')});
</script>

如何正确附加 select2 css 类?

【问题讨论】:

  • 你确定你是你页面加载的插件 select2.js 吗?
  • 是的,附加的图像都在同一个表单上,但集合类型字段不呈现它,实体字段类型确实呈现类。
  • 控制台对jQuery解决方案说了什么,也许它没有渲染。

标签: php css symfony select2


【解决方案1】:

试试这个:

->add('arrayDiagnosticos', 'collection', [
                'type' => new PreDiagnosticoType(),
                'label' => ' ',
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false,
                'attr' => array(
                        'class' => 'select2',
                ),
            ])

【讨论】:

  • 使用 [ ] 和 array() 有区别吗?
【解决方案2】:

由于您的&lt;selects /&gt; 是动态创建的,因此您必须设置一个侦听器以在创建每个选择元素时应用select2 函数。

这样的事情可能会起作用:

$('body').on('DOMNodeInserted', '.select2', function() {
    $(this).select2();
});

【讨论】:

  • 当我尝试这个时,浏览器崩溃了,我不知道为什么。当我尝试这个时:$('#paciente_form_arrayDiagnosticos').on('click', '.select2', function() { $(this).select2(); }); select2 出现在点击但数据丢失了
  • 我在 Firefox 上试了一下,没有崩溃,但是数据丢失了。无论如何感谢您的回答,它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多