I'm using a collectionType that render a multiple select inputs, I want to add the select2 css class to my form but it just doesn't works.
我正在使用一个呈现多个选择输入的collectionType,我想将select2 css类添加到我的表单中,但它只是不起作用。
This is the Form that has the collection.
这是具有该集合的表单。
->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',
],
],
])
The twig is
树枝是
<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>
It should render a select input like this: 1
它应该呈现如下的选择输入:1
But it renders like a regular select input 2
但它呈现为常规选择输入2
This is the output html
这是输出html
<div class="form-group"><label class="control-label required">Diagnósticos dinámicos</label>
<div id="paciente_form_arrayDiagnosticos" class="select2" data-prototype="<div class="row"><div class="col-xs-9"><select id="paciente_form_arrayDiagnosticos___name__" name="paciente_form[arrayDiagnosticos][__name__]" required="required" class="select2 form-control"><option value="" selected="selected">Seleccionar Diagnóstico</option> <option value="1" >se siente mal</option> <option value="2" >asfd</option> <option value="3" >YOLO</option></select></div><div class="col-xs-3"><a href="#" class="btn btn-danger btn-sm" data-removefield="collection" data-field="__id__">Eliminar Diagnóstico</a></div></div>" 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>
I also tried to do it via jQuery and no luck
我也尝试通过jQuery做到这一点,没有运气
<script>
$(function(){
$('#paciente_form_arrayDiagnosticos').addClass('select2')});
</script>
How do I properly attach the select2 css class?
如何正确附加select2 css类?
2 个解决方案
#1
1
Since your <selects />
are created dinamically, you have to set up a listener to apply the select2
function when each select element is created.
由于您的
Somthing like this might work:
这样的事情可能有用:
$('body').on('DOMNodeInserted', '.select2', function() {
$(this).select2();
});
#2
0
Try this:
尝试这个:
->add('arrayDiagnosticos', 'collection', [
'type' => new PreDiagnosticoType(),
'label' => ' ',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'attr' => array(
'class' => 'select2',
),
])
#1
1
Since your <selects />
are created dinamically, you have to set up a listener to apply the select2
function when each select element is created.
由于您的
Somthing like this might work:
这样的事情可能有用:
$('body').on('DOMNodeInserted', '.select2', function() {
$(this).select2();
});
#2
0
Try this:
尝试这个:
->add('arrayDiagnosticos', 'collection', [
'type' => new PreDiagnosticoType(),
'label' => ' ',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'attr' => array(
'class' => 'select2',
),
])