我在使用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="form-group">
<label class="control-label"for="team_players___name__">__name__label__</label>
<select id="team_players___name__" name="team[players][__name__]" 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 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="form-group">
<label class="control-label" for="team_players___name__">__name__label__</label>
<select id="team_players___name__" name="team[players][__name__]" 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>" 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>
感谢您的帮助,
答案 0 :(得分:1)
在设置原型模板的.js中,我添加了这一行并且它可以正常工作。
$container.find('select').each(function(){
$(this).select2();
});