我試圖做的是「嵌入表單集」如下所述:http://symfony.com/doc/2.3/cookbook/form/form_collections.html嵌入表單的多個字段 - Symfony2的JQuery的多個領域
我設法得到它設置正確,併到舞臺我添加JavaScript/JQuery它工作正常。不過,現在我已經添加了JQuery部分,我無法讓它正常工作。點擊添加產品一次之後
初始加載的窗體
形式。
正如您在上面看到的,當我點擊添加產品時,它將添加2套添加產品表單。這在它自己是不正確的。更糟的是,添加的2個新行中的第一行不會保留到數據庫中。每次我添加新產品時都會發生同樣的事情。
現在文檔只處理一個提交的'標籤',但我認爲只是適應教程,我的需要就足夠了。我在我的實體/映射/控制器和視圖中的每個地方都嘗試對此進行排序。想想我已經嘗試過把所有事情都做好,但卻做不到。除了JQuery代碼本身,因爲我對JQuery或Javascript一無所知(有足夠的工作能夠學習PHP,而不會讓事情複雜化,儘管我一旦掌握了Symfony就完全打算學習它)。
//配方形式
class RecipeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('recipename') ->add('recipedesc')
->add('recipeyeild') ->add('recipecost');
$builder->add('product', 'collection', array(
'type' => new ProductRecipeType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,));
}
// ProductRecipe表格
class ProductRecipeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('amount') ->add('product');
}
//配方視圖new.twig
<h1>Recipes creation</h1>
{{ form_start(form) }}
{{ form_row(form.recipename) }} {{ form_row(form.recipedesc) }}
{{ form_row(form.recipeyeild) }} {{ form_row(form.recipecost) }}
<h3>Products</h3>
<ul class="product" data-prototype="{{ form_widget(form.product.vars.prototype)|e }}">
{% for products in form.product %}
{{ form_row(products.amount) }}
{{ form_row(products.product) }}
{% endfor %}
</ul>
{{ form_end(form) }}
// JQuery的用於配方形式(目前在但是我確實希望將它移動到一個單獨的文件,一旦正常工作)
<script>
var $collectionHolder;
// setup an "add a product" link
var $addProductsLink = $('<a href="#" class="add_product_link">Add a product</a>');
var $newLinkLi = $('<li></li>').append($addProductsLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.product');
// add a delete link to all of the existing tag form li elements
$collectionHolder.find('li').each(function() {
addProductsFormDeleteLink($(this));
});
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addProductsLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addProductsForm($collectionHolder, $newLinkLi);
});
});
function addProductsForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
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(/__product__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
addProductsFormDeleteLink($newFormLi);
}
function addProductsFormDeleteLink($productsFormLi) {
var $removeFormA = $('<a href="#">Delete</a>');
$productsFormLi.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
$productsFormLi.remove();
});
}
現在我認爲這是導致該問題是這一行:
$collectionHolder.data('index', $collectionHolder.find(':input').length);
具體的.find(「:輸入」)部分如同我猜它是計數輸入領域,但這只是純粹的猜測工作。
另外,雖然我問這個問題,我也更喜歡它,如果'添加產品'鏈接沒有刪除按鈕,因爲點擊它時完全刪除窗體中的'添加產品'鏈接,唯一的重新獲得它的方式是刷新頁面。想想我已經涵蓋了一切。
編輯
這是從「查看源文件」(看起來有點噁心的說實話笑)
<ul class="product" data-prototype="<div
id="bc_inventorybundle_recipe_product___name__"><div><
label for="bc_inventorybundle_recipe_product___name___amount"
class="required">Amount</label><input type="
text" id="bc_inventorybundle_recipe_product___name___amount"
name="bc_inventorybundle_recipe[product][__name__][amount]"
required="required" /></div><div><label
for="bc_inventorybundle_recipe_product___name___product">Product<
/label><select id="bc_inventorybundle_recipe_product___name___product"
name="bc_inventorybundle_recipe[product][__name__][product]"><option
value=""></option><option
value="66">Butter</option><option
value="67">Beef</option><option
value="68">Jam</option><option value="69">Xanthan
Gum</option><option value="70">Test
Product</option><option
value="71">test</option><option
value="72">test</option><option
value="73">Beef</option><option
value="74">Beef</option></select></div></div>">