@@ -255,7 +255,7 @@ On the rendered page, the result will look something like this:
255
255
256
256
.. code-block :: html
257
257
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.bars|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> ; " >
259
259
260
260
.. seealso ::
261
261
@@ -290,19 +290,9 @@ functionality with JavaScript:
290
290
291
291
.. code-block :: javascript
292
292
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));
306
296
307
297
The ``addFormToCollection() `` function's job will be to use the ``data-prototype ``
308
298
attribute 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]``)
312
302
313
303
.. code-block :: javascript
314
304
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 );
321
307
322
- // get the new index
323
- var index = $collectionHolder .data (' index' );
308
+ const item = document .createElement (' li' );
324
309
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
+ );
330
317
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);
334
319
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
+ };
343
322
344
323
Now, each time a user clicks the ``Add a tag `` link, a new sub form will
345
324
appear 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:
552
531
553
532
.. code-block :: javascript
554
533
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
+ })
566
538
539
+ // ... the rest of the block from above
540
+
567
541
function addFormToCollection () {
568
542
// ...
569
543
570
544
// add a delete link to the new form
571
- addTagFormDeleteLink ($newFormLi );
545
+ addTagFormDeleteLink (item );
572
546
}
573
547
574
548
The ``addTagFormDeleteLink() `` function will look something like this:
575
549
576
550
.. code-block :: javascript
577
551
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);
581
558
582
- $removeFormButton .on (' click' , function (e ) {
559
+ removeFormButton .addEventListener (' click' , (e ) => {
560
+ e .preventDefault ()
583
561
// remove the li for the tag form
584
- $ tagFormLi .remove ();
562
+ tagFormLi .remove ();
585
563
});
586
564
}
587
565
@@ -677,7 +655,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
677
655
678
656
.. _`Owning Side and Inverse Side` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
679
657
.. _`jQuery` : http://jquery.com/
680
- .. _`JSFiddle` : http ://jsfiddle.net/847Kf/4 /
658
+ .. _`JSFiddle` : https ://jsfiddle.net/ey8ozh6n /
681
659
.. _`@a2lix/symfony-collection` : https://github.com/a2lix/symfony-collection
682
660
.. _`symfony-collection` : https://github.com/ninsuo/symfony-collection
683
661
.. _`ArrayCollection` : https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html
0 commit comments