我想为我的一个项目创建一个管理面板,所以我从本教程开始: How to Embed a Collection of Forms
继续创建双嵌入表单,所以我有一个对象,什么有对象,这些对象也有对象:D
Doctrine映射很好,并且有效,问题是:
当我单击“添加新属性”时,它会创建属性,但是当我单击“添加详细信息”(向属性添加细节)时,它会创建详细信息的字段,但也会创建一个额外的字段:
<label class="required">1label__</label>
表格类型代码:
对于主类
$builder
->add('forma')
;
$builder->add('properties', 'collection',array(
'type' => new PropertyType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
对于属性类:
$builder
->add('nev')
//->add('szokokut')
;
$builder->add('details', 'collection',array(
'type' => new DetailType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
对于Detail类:
$builder
->add('description')
//->add('property')
;
这些代码来自buildFrom()函数。
知道为什么会有额外的字段?
第一个原型:
<div id="szokokut_storebundle_szokokuttype_properties___name__"><div><label for="szokokut_storebundle_szokokuttype_properties___name___nev" class="required">Nev</label><input type="text" id="szokokut_storebundle_szokokuttype_properties___name___nev" name="szokokut_storebundle_szokokuttype[properties][__name__][nev]" required="required" maxlength="100" /></div><div><label class="required">Details</label><div id="szokokut_storebundle_szokokuttype_properties___name___details" data-prototype="<div><label class="required">__name__label__</label><div id="szokokut_storebundle_szokokuttype_properties___name___details___name__"><div><label for="szokokut_storebundle_szokokuttype_properties___name___details___name___description" class="required">Description</label><input type="text" id="szokokut_storebundle_szokokuttype_properties___name___details___name___description" name="szokokut_storebundle_szokokuttype[properties][__name__][details][__name__][description]" required="required" maxlength="100" /></div></div></div>"></div></div></div>
第二个:
<div><label class="required">1label__</label><div id="szokokut_storebundle_szokokuttype_properties_1_details_1"><div><label for="szokokut_storebundle_szokokuttype_properties_1_details_1_description" class="required">Description</label><input type="text" id="szokokut_storebundle_szokokuttype_properties_1_details_1_description" name="szokokut_storebundle_szokokuttype[properties][1][details][1][description]" required="required" maxlength="100" /></div></div></div>
问题出在这里:
它来自教程(稍作修改),它取代了两个原型中的__name__
字段:/
var $addPropertyLink = $('<a href="#" class="add_property_link">Tulajdonság hozzáadása</a>');
var $newLinkLi = $('<p></p>').append($addPropertyLink);
jQuery(document).ready(function() {
propertyHolder.append($newLinkLi);
$addPropertyLink.on('click', function(e) {
e.preventDefault();
addPropertyForm(propertyHolder, $newLinkLi);
});
});
function addPropertyForm(collectionHolder, $newLinkLi) {
// Get the data-prototype we explained earlier
var prototype = collectionHolder.attr('data-prototype');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on the current collection's length.
var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);
// Display the form in the page in an li
var $newFormLi = $('<p></p>').append(newForm);
$newLinkLi.before($newFormLi);
addFormDeleteLink($newFormLi);
var $addDetailLink = $('<a href="#" class="add_detail_link">Részlet hozzáadása</a>');
var $LinkLi = $('<p></p>').append($addDetailLink);
$newFormLi.find('div[id$=_details]').append($LinkLi);
$addDetailLink.on('click',function(e){
e.preventDefault();
addDetailForm($newFormLi.find('div[id$=_details]'),$LinkLi);
});
}
function addFormDeleteLink($FormLi) {
var $removeFormA = $('<a href="#">törlés</a>');
$FormLi.append($removeFormA);
$removeFormA.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// remove the li for the tag form
$FormLi.remove();
});
}
function addDetailForm(collectionHolder,$newLinkLi){
// Get the data-prototype we explained earlier
var prototype = collectionHolder.attr('data-prototype');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on the current collection's length.
var newForm = prototype.replace(/__name__/g, collectionHolder.children().length);
// Display the form in the page in an li
var $newFormLi = $('<p></p>').append(newForm);
$newLinkLi.before($newFormLi);
addFormDeleteLink($newFormLi);
}
答案 0 :(得分:0)
您应该尝试通过使用足够好的选择器来确保您的javascript仅作用于正确的元素集。 显示你的js以获得更详细的答案。
<强>更新强> 看the code
似乎有一个未记录的prototype_name
选项,您可以从名称更改为您想要的其中一个原型。