Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Changing the "jQuery" part#13450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
ThomasLandauer wants to merge2 commits intosymfony:4.4fromThomasLandauer:patch-24
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletionsform/form_collections.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -304,52 +304,38 @@ On the rendered page, the result will look something like this:

<ul class="tags" data-prototype="&lt;div&gt;&lt;label class=&quot; required&quot;&gt;__name__&lt;/label&gt;&lt;div id=&quot;task_tags___name__&quot;&gt;&lt;div&gt;&lt;label for=&quot;task_tags___name___name&quot; class=&quot; required&quot;&gt;Name&lt;/label&gt;&lt;input type=&quot;text&quot; id=&quot;task_tags___name___name&quot; name=&quot;task[tags][__name__][name]&quot; required=&quot;required&quot; maxlength=&quot;255&quot; /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;">

The goal of this section will be to use JavaScript to read this attribute
and dynamically add new tag forms when the user clicks a "Add a tag" link.
This example uses jQuery and assumes you have it included somewhere on your page.
Now we need some JavaScript to read this attribute and dynamically add new tag forms
when the user clicks an "Add a tag" link. This example uses `jQuery`_.

Add a ``script`` tag somewhereon yourpage so you can start writing some JavaScript.
First, add the link somewherein yourtemplate:

First, add a link to the bottom of the "tags" list via JavaScript. Second,
bind to the "click" event of that link so you can add a new tag form (``addTagForm()``
will be show next):
.. code-block:: html+twig

.. code-block:: javascript
{# templates/task/new.html.twig #}

{# ... #}

{{ form_start(form) }}
{# ... #}
<a href="#" class="add_tag_link">Add a tag</a>
{{ form_end(form) }}

var $collectionHolder;
Now add the required functionality with #"444fb88ada4cee72a75a216de75c8b538a344b8f5e8e8e138b2ca2887e09a313">
// setup an "add a tag" link
var $addTagButton = $('<button type="button" class="add_tag_link">Add a tag</button>');
var $newLinkLi = $('<li></li>').append($addTagButton);
The ``data-prototype`` HTML contains the tag's ``text`` input element with a name of
``task[tags][__name__][name]`` and id of ``task_tags___name___name``. The ``__name__``
part is a "placeholder", which is replaced by a unique, incrementing number
(e.g. ``task[tags][3][name]``).

jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.tags');

// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);

// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
.. code-block:: javascript

$addTagButton.on('click', function(e) {
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
$(document).ready(function() {
$('.add_tag_link').click(function(e) {
e.preventDefault();
addTagForm();
});
});

The ``addTagForm()`` function's job will be to use the ``data-prototype`` attribute
to dynamically add a new form when this link is clicked. The ``data-prototype``
HTML contains the tag ``text`` input element with a name of ``task[tags][__name__][name]``
and id of ``task_tags___name___name``. The ``__name__`` is a little "placeholder",
which you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``).

The actual code needed to make this all work can vary quite a bit, but here's
one example:

.. code-block:: javascript

function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
Expand DownExpand Up@@ -716,6 +702,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
the `symfony-collection`_ package based on jQuery for the rest of browsers.

.. _`Owning Side and Inverse Side`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
.. _`jQuery`: http://jquery.com/
.. _`JSFiddle`: http://jsfiddle.net/847Kf/4/
.. _`@a2lix/symfony-collection`: https://github.com/a2lix/symfony-collection
.. _`symfony-collection`: https://github.com/ninsuo/symfony-collection

[8]ページ先頭

©2009-2025 Movatter.jp