@@ -255,7 +255,7 @@ On the rendered page, the result will look something like this:
255255
256256.. code-block :: html
257257
258- <ul class =" tags" 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> ; " >
258+ <ul class =" tags" data-index = " {{ form.tags|length > 0 ? form.tags|last.vars.name + 1 : 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> ; " >
259259
260260.. seealso ::
261261
@@ -290,19 +290,9 @@ functionality with JavaScript:
290290
291291.. code-block :: javascript
292292
293- jQuery (document ).ready (function () {
294- // Get the ul that holds the collection of tags
295- var $tagsCollectionHolder = $ (' ul.tags' );
296- // count the current form inputs we have (e.g. 2), use that as the new
297- // index when inserting a new item (e.g. 2)
298- $tagsCollectionHolder .data (' index' , $tagsCollectionHolder .find (' input' ).length );
299-
300- $ (' body' ).on (' click' , ' .add_item_link' , function (e ) {
301- var $collectionHolderClass = $ (e .currentTarget ).data (' collectionHolderClass' );
302- // add a new tag form (see next code block)
303- addFormToCollection ($collectionHolderClass);
304- })
305- });
293+ document
294+ .querySelectorAll (' .add_item_link' )
295+ .forEach (btn => btn .addEventListener (" click" , addFormToCollection));
306296
307297 The ``addFormToCollection() `` function's job will be to use the ``data-prototype ``
308298attribute to dynamically add a new form when this link is clicked. The ``data-prototype ``
@@ -312,34 +302,23 @@ you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``)
312302
313303.. code-block :: javascript
314304
315- function addFormToCollection ($collectionHolderClass ) {
316- // Get the ul that holds the collection of tags
317- var $collectionHolder = $ (' .' + $collectionHolderClass);
318-
319- // Get the data-prototype explained earlier
320- var prototype = $collectionHolder .data (' prototype' );
305+ const addFormToCollection = (e ) => {
306+ const collectionHolder = document .querySelector (' .' + e .currentTarget .dataset .collectionHolderClass );
321307
322- // get the new index
323- var index = $collectionHolder .data (' index' );
308+ const item = document .createElement (' li' );
324309
325- var newForm = prototype;
326- // You need this only if you didn't set 'label' => false in your tags field in TaskType
327- // Replace '__name__label__' in the prototype's HTML to
328- // instead be a number based on how many items we have
329- // newForm = newForm.replace(/__name__label__/g, index);
310+ item .innerHTML = collectionHolder
311+ .dataset
312+ .prototype
313+ .replace (
314+ / __name__/ g ,
315+ collectionHolder .dataset .index
316+ );
330317
331- // Replace '__name__' in the prototype's HTML to
332- // instead be a number based on how many items we have
333- newForm = newForm .replace (/ __name__/ g , index);
318+ collectionHolder .appendChild (item);
334319
335- // increase the index with one for the next item
336- $collectionHolder .data (' index' , index + 1 );
337-
338- // Display the form in the page in an li, before the "Add a tag" link li
339- var $newFormLi = $ (' <li></li>' ).append (newForm);
340- // Add the new form at the end of the list
341- $collectionHolder .append ($newFormLi)
342- }
320+ collectionHolder .dataset .index ++ ;
321+ };
343322
344323 Now, each time a user clicks the ``Add a tag `` link, a new sub form will
345324appear on the page. When the form is submitted, any new tag forms will be converted
@@ -552,36 +531,35 @@ First, add a "delete this tag" link to each tag form:
552531
553532.. code-block :: javascript
554533
555- jQuery (document ).ready (function () {
556- // Get the ul that holds the collection of tags
557- $collectionHolder = $ (' ul.tags' );
558-
559- // add a delete link to all of the existing tag form li elements
560- $collectionHolder .find (' li' ).each (function () {
561- addTagFormDeleteLink ($ (this ));
562- });
563-
564- // ... the rest of the block from above
565- });
534+ const tags = document .querySelectorAll (' ul.tags' )
535+ tags .forEach ((tag ) => {
536+ addTagFormDeleteLink (tag)
537+ })
566538
539+ // ... the rest of the block from above
540+
567541 function addFormToCollection () {
568542 // ...
569543
570544 // add a delete link to the new form
571- addTagFormDeleteLink ($newFormLi );
545+ addTagFormDeleteLink (item );
572546 }
573547
574548 The ``addTagFormDeleteLink() `` function will look something like this:
575549
576550.. code-block :: javascript
577551
578- function addTagFormDeleteLink ($tagFormLi ) {
579- var $removeFormButton = $ (' <button type="button">Delete this tag</button>' );
580- $tagFormLi .append ($removeFormButton);
552+ const addTagFormDeleteLink = (tagFormLi ) => {
553+ const removeFormButton = document .createElement (' button' )
554+ removeFormButton .classList
555+ removeFormButton .innerText = ' Delete this tag'
556+
557+ tagFormLi .append (removeFormButton);
581558
582- $removeFormButton .on (' click' , function (e ) {
559+ removeFormButton .addEventListener (' click' , (e ) => {
560+ e .preventDefault ()
583561 // remove the li for the tag form
584- $ tagFormLi .remove ();
562+ tagFormLi .remove ();
585563 });
586564 }
587565
@@ -677,7 +655,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
677655
678656.. _`Owning Side and Inverse Side` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
679657.. _`jQuery` : http://jquery.com/
680- .. _`JSFiddle` : http ://jsfiddle.net/847Kf/4 /
658+ .. _`JSFiddle` : https ://jsfiddle.net/ey8ozh6n /
681659.. _`@a2lix/symfony-collection` : https://github.com/a2lix/symfony-collection
682660.. _`symfony-collection` : https://github.com/ninsuo/symfony-collection
683661.. _`ArrayCollection` : https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html
0 commit comments