我是symfony的新手,我已从实体接收数据到我的控制器并将其发送到我的twig模板现在我想使用集合原型显示该数据,以便我可以更新该数据,
我用于创建集合proto的js代码
custom.js
function addTagForm(collectionHolder, newLinkLi) {
var prototype = collectionHolder.data('prototype');
// get the new index
var index = collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
collectionHolder.data('index', index + 1);
if(collectionHolder.data('index') <= 6){
// Display the form in the page in an li, before the "Add a tag" link li
var newFormLi = $('<li style="width:100%"></li>').append(newForm);
newLinkLi.before(newFormLi);
//alert(newFormLi);
}else{
$(newLinkLi).remove();
}
}
});
我的枝条将其渲染成html
<div class="form-group">
<label class="col-sm-2 control-label required" for="product_about_product">Reference List</label><div class="col-sm-10">
<ul class="c4" data-prototype="{{ form_widget(form.ReferenceLists.vars.prototype)|e }}">
</ul>
</div>
</div>
答案 0 :(得分:2)
Prototype用于通过javascript动态创建新表单项。要显示现有的集合项,您需要遍历集合类型子表单:
<ul>
{% for child in form.ReferenceLists %}
<li>{{ form_widget(child) }}</li>
{% endfor
</ul>