我在Symfony中有一個Web應用程序。當我有一個對象集合(一個用戶有很多地址)時,我需要實現JavaScript/jQuery方法來讓用戶添加他擁有的地址數量(https://symfony.com/doc/current/form/form_collections.html)。Symfony/Jquery對象集合
問題是,我想包裝每個標籤和每個輸入標籤與<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12"></div>
特殊格,但經過多次嘗試我不能這樣做。你可以幫我嗎 ?
我的代碼:
<div class="row">
<div id="addressList" data-prototype="{{ form_widget(parent_form.addresses.vars.prototype)|e }}">
</div>
</div>
在data-prototype="..."
的{{ form_widget(parent_form.addresses.vars.prototype)|e }}
產生這個網站:
<div id="AddChildStep1_child_addresses___name__">
<div class="form-group">
<label class="control-label required" for="AddChildStep1_child_addresses___name___street1">Street1</label>
<input type="text" id="AddChildStep1_child_addresses___name___street1" name="AddChildStep1[child][addresses][__name__][street1]" required="required" maxlength="100" pattern=".{5,}" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="AddChildStep1_child_addresses___name___street2">Street2</label>
<input type="text" id="AddChildStep1_child_addresses___name___street2" name="AddChildStep1[child][addresses][__name__][street2]" maxlength="100" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="AddChildStep1_child_addresses___name___number">Number</label>
<input type="number" id="AddChildStep1_child_addresses___name___number" name="AddChildStep1[child][addresses][__name__][number]" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="AddChildStep1_child_addresses___name___box">Box</label>
<input type="number" id="AddChildStep1_child_addresses___name___box" name="AddChildStep1[child][addresses][__name__][box]" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="AddChildStep1_child_addresses___name___locality">Locality</label>
<select id="AddChildStep1_child_addresses___name___locality" name="AddChildStep1[child][addresses][__name__][locality]" class="form-control">
<option value=""></option>
<option value="1">1080 - Molenbeek-Saint-Jean - BELGIUM</option>
<option value="2">1060 - Saint-Gilles - BELGIUM</option>
<option value="3">1050 - Ixelles - BELGIUM</option>
</select>
</div>
<div class="form-group">
<label class="control-label required" for="AddChildStep1_child_addresses___name___addressType">Address type</label>
<select id="AddChildStep1_child_addresses___name___addressType" name="AddChildStep1[child][addresses][__name__][addressType]" class="form-control">
<option value="HOME">Home</option>
<option value="WORK">Work</option>
<option value="HOLIDAYS">Holidays</option>
<option value="OTHER">Other</option>
</select>
</div>
</div>
然後Symfony的文檔提議該代碼添加一個地址:
var collectionHolder;
// Set up an "add address" link
var addAddressLink = $('<a href="#" class="add_address_link">Add address</a>');
var newLinkP = $('<p class="centered"></p>').append(addAddressLink);
function addAddressForm(collectionHolder, newLinkP){
// Get the data prototype
var prototype = collectionHolder.data('prototype');
// get the new index
var index = collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML
//instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// Increase the index with one for the new item
collectionHolder.data('index', index+1);
//Display the form in the page nan li, before the "add address" link
var newFormP = $('<div class="one-address"></div>').append(newForm);
newLinkP.before(newFormP)
}
jQuery(document).ready(function(){
// Get the div that holds the collection of addresses
collectionHolder = $('div#addressList');
// add the "add address" anchor
collectionHolder.append(newLinkP);
// Count the current form inputs
// use that as the new index when inserting a new item
collectionHolder.data('index', collectionHolder.find(':input').length);
addAddressLink.on('click', function(e)
{
// Prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new address form
addAddressForm(collectionHolder, newLinkP);
})
});
我想用<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12></div>
包裝每個標籤元素和每個輸入元素(即在div.one-address
中)。
謝謝!我做了一個自定義的'form_row',它簡化了我的生活! –