Select2 with collectionType - Symfony2

时间:2016-05-10 12:31:25

标签: javascript jquery symfony select2

我在使用Symfony2中的集合类型的select2时遇到问题

当我使用基本选择时,我可以选择,但是当我使用select2时,没有找到结果'。此问题特别适用于集合类型(textType,emailType工作)。

我不明白这种行为。

修改

这是我的teamType:

$builder->add('players', 'collection', array(
            'type' => 'entity',
            'allow_add' => true,
            'allow_delete' => true,
            ...
        ))

这是我的选择2:

$('#team_players').select2(
        {
         placeholder: '{{ 'Select player' }}'
        });

my js for display collection type:

 $(document).ready(function()
    {
     var container = $('#team_players');
     var index = 0;

     // HTML output: <select id="team_players_0" name="[players][0]" class="form-control">...

     var prototype = $(container.attr('data-prototype').replace(/__name__label__/g, 'choose').replace(/__name__/g, index)); 
     container.append(prototype);
    });

修改

我的html输出没有select2:

<div class="team_players_widget">
<div id="team_players" class="my-players" 
    data-prototype="<div class=&quot;form-group&quot;>
                        <label class=&quot;control-label&quot;for=&quot;team_players___name__&quot;>__name__label__</label>
                        <select id=&quot;team_players___name__&quot; name=&quot;team[players][__name__]&quot; class=&quot;form-control&quot;>
                            <option value=&quot;&quot;></option>            
                            <option value=&quot;1&quot; >Player 1</option>            
                            <option value=&quot;2&quot; >Player 2</option>            
                            <option value=&quot;3&quot; >PLayer 3</option>
                        </select>
                    </div>">

    <div class="form-group">
        <label class="control-label" for="team_players_0">choose</label>
            <select id="team_players_0" name="team[players][0]" class="form-control">
                <option value=""></option>            
                <option value="1">Player 1</option>            
                <option value="2">Player 2</option>            
                <option value="3">Player 3</option></select>
    </div>
</div>

我的html输出与select2:

<div class="team_players_widget">
<div id="team_players" class="my-players select2-hidden-accessible" 
    data-prototype="<div class=&quot;form-group&quot;>
    <label class=&quot;control-label&quot; for=&quot;team_players___name__&quot;>__name__label__</label>
    <select id=&quot;team_players___name__&quot; name=&quot;team[players][__name__]&quot; class=&quot;form-control&quot;>
        <option value=&quot;&quot;></option>            
        <option value=&quot;1&quot; >Player 1</option>            
        <option value=&quot;2&quot; >Player 2</option>            
        <option value=&quot;3&quot; >Player 3</option>
    </select>
</div>" tabindex="-1" aria-hidden="true">

<div class="form-group">
    <label class="control-label" for="team_players_0">choose</label>
    <select id="team_players_0" name="team[players][0]" class="form-control">
        <option value=""></option>            
        <option value="1">Player 1</option>            
        <option value="2">Player 2</option>            
        <option value="3">Player 3</option></select>
</div>

<span class="select2 select2-container select2-container--default select2-container--below select2-container--open select2-container--focus" dir="ltr" style="width: 100%;">
        <span class="selection">
            <span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="true" tabindex="0" aria-labelledby="select2-team_players-container" aria-owns="select2-team_players-results">
                <span class="select2-selection__rendered" id="select2-team_players-container">
                    <span class="select2-selection__placeholder">Select player</span>
                </span>
                <span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span>
            </span>
        </span>
                <span class="dropdown-wrapper" aria-hidden="true"></span>
    </span>
 </div>  

感谢您的帮助,

1 个答案:

答案 0 :(得分:1)

在设置原型模板的.js中,我添加了这一行并且它可以正常工作。

$container.find('select').each(function(){
  $(this).select2();
});