4

我正在使用呈现多个选择输入的 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>

它应该呈现这样的选择输入: 使用 select2 类1

但它呈现像常规选择输入 没有 select2 类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 类?

4

2 回答 2

1

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

像这样的东西可能会起作用:

$('body').on('DOMNodeInserted', '.select2', function() {
    $(this).select2();
});
于 2016-01-20T18:22:14.503 回答
1

尝试这个:

->add('arrayDiagnosticos', 'collection', [
                'type' => new PreDiagnosticoType(),
                'label' => ' ',
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false,
                'attr' => array(
                        'class' => 'select2',
                ),
            ])
于 2015-12-31T13:46:38.487 回答