@@ -158,6 +158,7 @@ things are even easier because the ``data-prototype`` attribute is rendered
158
158
automatically for you (with a slight difference - see note below) and all
159
159
you need is the JavaScript:
160
160
161
+
161
162
.. configuration-block ::
162
163
163
164
.. code-block :: html+twig
@@ -167,7 +168,8 @@ you need is the JavaScript:
167
168
168
169
{# store the prototype on the data-prototype attribute #}
169
170
<ul id="email-fields-list"
170
- data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}">
171
+ data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
172
+ data-widget-tags="{{ '<li></li>'|e }}">
171
173
{% for emailField in form.emails %}
172
174
<li>
173
175
{{ form_errors(emailField) }}
@@ -176,35 +178,43 @@ you need is the JavaScript:
176
178
{% endfor %}
177
179
</ul>
178
180
179
- <a href="#" id="add-another-email">Add another email</a>
181
+ <a href="#"
182
+ class="add-another-collection-widget"
183
+ data-list="#email-field-list>Add another email</a>
180
184
181
185
{# ... #}
182
186
{{ form_end(form) }}
183
187
184
- <script type="text/javascript">
185
- // keep track of how many email fields have been rendered
186
- var emailCount = '{{ form.emails|length }}';
187
-
188
- jQuery(document).ready(function() {
189
- jQuery('#add-another-email').click(function(e) {
190
- e.preventDefault();
191
-
192
- var emailList = jQuery('#email-fields-list');
193
-
194
- // grab the prototype template
195
- var newWidget = emailList.attr('data-prototype');
196
- // replace the "__name__" used in the id and name of the prototype
197
- // with a number that's unique to your emails
198
- // end name attribute looks like name="contact[emails][2]"
199
- newWidget = newWidget.replace(/__name__/g, emailCount);
200
- emailCount++;
201
-
202
- // create a new list element and add it to the list
203
- var newLi = jQuery('<li></li>').html(newWidget);
204
- newLi.appendTo(emailList);
205
- });
206
- })
207
- </script>
188
+ <script src="add-collection-widget.js"></script>
189
+
190
+ .. code-block :: javascript
191
+
192
+ // add-collection-widget.js
193
+ jQuery (document ).ready (function () {
194
+ jQuery (' .add-another-collection-widget' ).click (function (e ) {
195
+ e .preventDefault ();
196
+ var list = jQuery (jQuery (this ).attr (' data-list' ));
197
+ // Try to find the counter of the list
198
+ var counter = list .data (' widget-counter' );
199
+ // If the counter does not exist, use the length of the list
200
+ if (! counter) { counter = list .children ().length ; }
201
+
202
+ // grab the prototype template
203
+ var newWidget = list .attr (' data-prototype' );
204
+ // replace the "__name__" used in the id and name of the prototype
205
+ // with a number that's unique to your emails
206
+ // end name attribute looks like name="contact[emails][2]"
207
+ newWidget = newWidget .replace (/ __name__/ g , counter);
208
+ // Increase the counter
209
+ counter++ ;
210
+ // And store it, the length cannot be used if deleting widgets is allowed
211
+ list .data (' widget-counter' , counter);
212
+
213
+ // create a new list element and add it to the list
214
+ var newElem = jQuery (list .attr (' data-widget-tags' )).html (newWidget);
215
+ newElem .appendTo (list);
216
+ });
217
+ });
208
218
209
219
.. tip ::
210
220
0 commit comments