我在Symfony2中将select2与集合类型一起使用时遇到问题

当我使用基本选择时,可以使用我的选项,但是当我使用select2时,“未找到结果”。此问题特别是与集合类型有关(textType,emailType工作)。

我不了解这种行为。

编辑:

这是我的teamType:

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

这是我的select2:
$('#team_players').select2(
        {
         placeholder: '{{ 'Select player' }}'
        });

我的显示集合类型的js:
 $(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>

我的带有select2的html输出:
<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>

谢谢你的帮助,

最佳答案

在设置原型(prototype)模板的.js文件中,我添加了这一行,它可以正常工作。

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

关于javascript - 具有collectionType的Select2-Symfony2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37138660/

10-13 02:29