Skip to content

Commit 43cb090

Browse files
author
marc.weistroff
committed
[Form] Added doc for collection prototype
1 parent 93c0a9f commit 43cb090

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

book/forms.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,31 @@ the following inside ``ProductType``:
917917
));
918918
}
919919
920+
The collection type also give you the ability to embed a prototype of
921+
the collection form in your view. The prototype is a row of the collection
922+
without any data in it that serves as a template to add more items.
923+
To do so, you must set the ``prototype`` and ``allow_add`` options to ``true``.
924+
925+
When your collection will be rendered, the html element containing your
926+
collection (div or table) will have a ``data-prototype`` attribute containing
927+
the rendered prototype, as a string. In order for you to display the
928+
prototype, you'll have to write a small JS to capture the attribute
929+
and insert it in the form. Here is a sample that you will have to adapt.
930+
931+
.. code-block:: javascript
932+
<script type="text/javascript">
933+
jQuery('#add-element').click(function() {
934+
var prototype = $('form#my_form #my_collection').attr('data-prototype');
935+
936+
// the level of the prototype is replaced by $$name$$
937+
// you have to change this to the number of items + 1
938+
prototype = prototype.replace(/\$\$name\$\$/g, '00');
939+
940+
// Append the prototype to the DOM
941+
$('#my_prototype').append(prototype);
942+
});
943+
</script>
944+
920945
.. index::
921946
single: Forms; Theming
922947
single: Forms; Customizing fields

0 commit comments

Comments
 (0)