@@ -238,25 +238,26 @@ it will receive an *unknown* number of tags. Otherwise, you'll see a
238
238
239
239
The ``allow_add `` option also makes a ``prototype `` variable available to you.
240
240
This "prototype" is a little "template" that contains all the HTML needed to
241
- dynamically create any new "tag" forms with JavaScript. To render the prototype, add
242
- the following ``data-prototype `` attribute to the existing ``<ul> `` in your template:
241
+ dynamically create any new "tag" forms with JavaScript. Now add the following
242
+ ``data-index `` (containing the current "form row" number for the following JavaScript)
243
+ and ``data-prototype `` attribute to the existing ``<ul> `` in your template:
243
244
244
245
.. code-block :: html+twig
245
246
246
247
<ul class="tags" data-index="{{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 0 }}" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"></ul>
247
248
248
- Now add a button just next to the ``<ul> `` to dynamically add a new tag:
249
-
250
- .. code-block :: html+twig
251
-
252
- <button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
253
-
254
249
On the rendered page, the result will look something like this:
255
250
256
251
.. code-block :: html
257
252
258
253
<ul class =" tags" data-index =" 0" data-prototype =" < ; div> ;< ; label class=" ; required" ;> ; __name__< ; /label> ;< ; div id=" ; task_tags___name__" ;> ;< ; div> ;< ; label for=" ; task_tags___name___name" ; class=" ; required" ;> ; Name< ; /label> ;< ; input type=" ; text" ; id=" ; task_tags___name___name" ; name=" ; task[tags][__name__][name]" ; required=" ; required" ; maxlength=" ; 255" ; /> ;< ; /div> ;< ; /div> ;< ; /div> ; " >
259
254
255
+ Now add a button to dynamically add a new tag:
256
+
257
+ .. code-block :: html+twig
258
+
259
+ <button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
260
+
260
261
.. seealso ::
261
262
262
263
If you want to customize the HTML code in the prototype, see
@@ -265,7 +266,7 @@ On the rendered page, the result will look something like this:
265
266
.. tip ::
266
267
267
268
The ``form.tags.vars.prototype `` is a form element that looks and feels just
268
- like the individual ``form_widget(tag) `` elements inside your ``for `` loop.
269
+ like the individual ``form_widget(tag.* ) `` elements inside your ``for `` loop.
269
270
This means that you can call ``form_widget() ``, ``form_row() `` or ``form_label() ``
270
271
on it. You could even choose to render only one of its fields (e.g. the
271
272
``name `` field):
@@ -303,17 +304,17 @@ you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``)
303
304
const addFormToCollection = (e ) => {
304
305
const collectionHolder = document .querySelector (' .' + e .currentTarget .dataset .collectionHolderClass );
305
306
306
- const item = document .createElement (' li' );
307
+ const element = document .createElement (' li' );
307
308
308
- item .innerHTML = collectionHolder
309
+ element .innerHTML = collectionHolder
309
310
.dataset
310
311
.prototype
311
312
.replace (
312
313
/ __name__/ g ,
313
314
collectionHolder .dataset .index
314
315
);
315
316
316
- collectionHolder .appendChild (item );
317
+ collectionHolder .appendChild (element );
317
318
318
319
collectionHolder .dataset .index ++ ;
319
320
};
@@ -540,24 +541,23 @@ First, add a "delete this tag" link to each tag form:
540
541
// ...
541
542
542
543
// add a delete link to the new form
543
- addTagFormDeleteLink (item );
544
+ addTagFormDeleteLink (element );
544
545
}
545
546
546
547
The ``addTagFormDeleteLink() `` function will look something like this:
547
548
548
549
.. code-block :: javascript
549
550
550
- const addTagFormDeleteLink = (tagFormLi ) => {
551
- const removeFormButton = document .createElement (' button' )
552
- removeFormButton .classList
553
- removeFormButton .innerText = ' Delete this tag'
551
+ const addTagFormDeleteLink = (element ) => {
552
+ const removeFormButton = document .createElement (' button' );
553
+ removeFormButton .innerText = ' Delete this tag' ;
554
554
555
- tagFormLi .append (removeFormButton);
555
+ element .append (removeFormButton);
556
556
557
557
removeFormButton .addEventListener (' click' , (e ) => {
558
- e .preventDefault ()
558
+ e .preventDefault ();
559
559
// remove the li for the tag form
560
- tagFormLi .remove ();
560
+ element .remove ();
561
561
});
562
562
}
563
563
@@ -652,7 +652,6 @@ the relationship between the removed ``Tag`` and ``Task`` object.
652
652
the `symfony-collection `_ package based on `jQuery `_ for the rest of browsers.
653
653
654
654
.. _`Owning Side and Inverse Side` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
655
- .. _`jQuery` : http://jquery.com/
656
655
.. _`JSFiddle` : https://jsfiddle.net/ey8ozh6n/
657
656
.. _`@a2lix/symfony-collection` : https://github.com/a2lix/symfony-collection
658
657
.. _`symfony-collection` : https://github.com/ninsuo/symfony-collection
0 commit comments