Skip to content

Commit 602ed85

Browse files
authored
Remove jQuery and add delete button
1 parent 2d10102 commit 602ed85

File tree

1 file changed

+23
-70
lines changed

1 file changed

+23
-70
lines changed

form/form_collections.rst

Lines changed: 23 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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``
308298
attribute to dynamically add a new form when this link is clicked. The ``data-prototype``
@@ -312,42 +302,10 @@ 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');
321-
322-
// get the new index
323-
var index = $collectionHolder.data('index');
324-
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);
305+
const addFormToCollection = (e) => {
306+
const collectionHolder = document.querySelector('.' + e.currentTarget.dataset.collectionHolderClass);
330307
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);
334-
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-
}
343-
344-
Code without jQuery library
345-
346-
.. code-block:: javascript
347-
const newItem = (e) => {
348-
const collectionHolder = document.querySelector(`.${e.currentTarget.dataset.collectionHolderClass}`);
349-
350-
const item = document.createElement("div");
308+
const item = document.createElement('li');
351309
352310
item.innerHTML = collectionHolder
353311
.dataset
@@ -362,10 +320,6 @@ Code without jQuery library
362320
collectionHolder.dataset.index++;
363321
};
364322
365-
document
366-
.querySelectorAll('.add_item_link')
367-
.forEach(btn => btn.addEventListener("click", newItem));
368-
369323
Now, each time a user clicks the ``Add a tag`` link, a new sub form will
370324
appear on the page. When the form is submitted, any new tag forms will be converted
371325
into new ``Tag`` objects and added to the ``tags`` property of the ``Task`` object.
@@ -577,36 +531,35 @@ First, add a "delete this tag" link to each tag form:
577531

578532
.. code-block:: javascript
579533
580-
jQuery(document).ready(function() {
581-
// Get the ul that holds the collection of tags
582-
$collectionHolder = $('ul.tags');
583-
584-
// add a delete link to all of the existing tag form li elements
585-
$collectionHolder.find('li').each(function() {
586-
addTagFormDeleteLink($(this));
587-
});
588-
589-
// ... the rest of the block from above
590-
});
534+
const tags = document.querySelectorAll('ul.tags')
535+
tags.forEach((tag) => {
536+
addTagFormDeleteLink(tag)
537+
})
591538
539+
// ... the rest of the block from above
540+
592541
function addFormToCollection() {
593542
// ...
594543
595544
// add a delete link to the new form
596-
addTagFormDeleteLink($newFormLi);
545+
addTagFormDeleteLink(item);
597546
}
598547
599548
The ``addTagFormDeleteLink()`` function will look something like this:
600549

601550
.. code-block:: javascript
602551
603-
function addTagFormDeleteLink($tagFormLi) {
604-
var $removeFormButton = $('<button type="button">Delete this tag</button>');
605-
$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);
606558
607-
$removeFormButton.on('click', function(e) {
559+
removeFormButton.addEventListener('click', (e) => {
560+
e.preventDefault()
608561
// remove the li for the tag form
609-
$tagFormLi.remove();
562+
tagFormLi.remove();
610563
});
611564
}
612565
@@ -702,7 +655,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
702655

703656
.. _`Owning Side and Inverse Side`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
704657
.. _`jQuery`: http://jquery.com/
705-
.. _`JSFiddle`: http://jsfiddle.net/847Kf/4/
658+
.. _`JSFiddle`: https://jsfiddle.net/ey8ozh6n/
706659
.. _`@a2lix/symfony-collection`: https://github.com/a2lix/symfony-collection
707660
.. _`symfony-collection`: https://github.com/ninsuo/symfony-collection
708661
.. _`ArrayCollection`: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html

0 commit comments

Comments
 (0)