Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5594d7e

Browse filesBrowse files
authored
Move JavaScript to seperate file
By creating a more general JavaScript code and putting it in a separate file, it can be used for all collections. This makes the code more DRY.
1 parent 0dbeb2c commit 5594d7e
Copy full SHA for 5594d7e

File tree

Expand file treeCollapse file tree

1 file changed

+36
-26
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-26
lines changed

‎reference/forms/types/collection.rst

Copy file name to clipboardExpand all lines: reference/forms/types/collection.rst
+36-26Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ things are even easier because the ``data-prototype`` attribute is rendered
158158
automatically for you (with a slight difference - see note below) and all
159159
you need is the JavaScript:
160160

161+
161162
.. configuration-block::
162163

163164
.. code-block:: html+twig
@@ -167,7 +168,8 @@ you need is the JavaScript:
167168

168169
{# store the prototype on the data-prototype attribute #}
169170
<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 }}">
171173
{% for emailField in form.emails %}
172174
<li>
173175
{{ form_errors(emailField) }}
@@ -176,35 +178,43 @@ you need is the JavaScript:
176178
{% endfor %}
177179
</ul>
178180
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>
180184

181185
{# ... #}
182186
{{ form_end(form) }}
183187

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+
});
208218
209219
.. tip::
210220

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.