-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
191 lines (140 loc) · 5.67 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
django-formset-js
=================
A wrapper for a JavaScript formset helper.
Installing
----------
Install via pip::
pip install django-formset-js
Then add it and its dependancy ``django-jquery-js``
to your ``INSTALLED_APPS``::
INSTALLED_APPS += (
'django.contrib.staticfiles',
'jquery',
'djangoformsetjs',
)
Using
-----
Include the JavaScript library
******************************
Both jQuery and this library must be included in your page.
The simplest way to do this is to add the scripts as media dependencies on your form::
from djangoformsetjs.utils import formset_media_js
class MyForm(forms.Form):
class Media(object):
js = formset_media_js + (
# Other form media here
)
MyFormSet = formset_factory(MyForm)
And then include the Media of the form in your template::
{{ formset.media }}
Alternatively, simply add the script tags::
<script src="{{ STATIC_URL }}js/jquery.js"></script>
<script src="{{ STATIC_URL }}js/jquery.formset.js"></script>
Render the formset
******************
So that the library can work with your formset,
certain blocks of your formset need to be marked up with ``data-formset-...`` attributes::
{% load formset_tags %}
<div id="formset" data-formset-prefix="{{ formset.prefix }}">
{{ formset.management_form }}
<div data-formset-body>
<!-- New forms will be inserted in here -->
{% for form in formset %}
<div data-formset-form>
{{ form }}
<button type="button" data-formset-delete-button>Delete form</button>
</div>
{% endfor %}
</div>
<!-- The empty form template. By wrapping this in a <script> tag, the
__prefix__ placeholder can easily be replaced in both attributes and
any scripts -->
<script type="form-template" data-formset-empty-form>
{% escapescript %}
<div data-formset-form>
{{ formset.empty_form }}
<button type="button" data-formset-delete-button>Delete form</button>
</div>
{% endescapescript %}
</script>
<!-- This button will add a new form when clicked -->
<input type="button" value="Add another" data-formset-add>
<script>jQuery(function($) {
$("#formset").formset({
animateForms: true
});
});</script>
</div>
The ``data-formset-`` data attributes are:
``data-formset-prefix``
The value of ``{{ formset.prefix }}``.
This is used to find the management form.
``data-formset-body``
This indicates where all the child forms are.
New forms are inserted in here.
``data-formset-form``
Every form (including the empty form) should have this attribute.
``data-formset-empty-form``
The element that contains the empty form template.
For best results, use a ``<script>`` tag.
``data-formset-add``
A button that adds a new form.
``data-formset-delete-button``
A button that deletes that form.
The empty form template is wrapped in a ``<script>`` as plain text.
This stops any JavaScript attached to widgets from running upon page load,
and makes finding and replacing the ``__prefix__`` placeholder easier.
The contents of the ``<script>`` should be wrapped in a ``{% escapescript %}`` block
to prevent any script tags inside from closing the wrapping script tag prematurely.
If the forms can be deleted, and contain a delete checkbox,
the following actions occur:
* When the checkbox is checked, marking the form for deletion,
the ``formDeleted`` event is fired on the ``data-formset-form`` container,
and the ``data-formset-form-deleted`` attribute is added.
* When the checkbox is unchecked, marking the form as active again,
the ``formAdded`` event is fired on the ``data-formset-form`` container,
and the ``data-formset-form-deleted`` attribute is removed.
If the forms can be deleted, and contain a delete button,
pressing the delete button will toggle the delete checkbox for that form.
The ``DELETE`` field should be hidden if the delete button is used.
The delete button is identified by the ``data-formset-delete-button`` attribute::
{% for form in formset %}
<div data-formset-form>
{{ form.name }}
{{ form.age }}
<div class="hidden">{{ form.DELETE }}</div>
<button type="button" data-formset-delete-button>Delete form</button>
</div>
{% endfor %}
If the ``animateForms`` option is set when the formset is created,
adding and deleting forms will be animated by sliding the forms in and out.
Options
*******
The jQuery plugin takes the following options:
``form``:
The selector to find forms.
Defaults to ``[data-formset-form]``.
``emptyForm``:
The selector to find the empty form template.
Defaults to ``script[type=form-template][data-formset-empty-form]``.
``body``:
The selector to find the formset body.
New forms will be inserted at the bottom of this element.
Defaults to ``[data-formset-body]``.
``add``:
The selector to find the add button.
Defaults to ``[data-formset-add]``.
``deleteButton``:
The selector to find the delete button within a form.
Defaults to ``[data-formset-delete-button]``.
``hasMaxFormsClass``:
The class added to the formset when the maximum number of forms is reached.
The maximum number of forms is pulled from the management form.
Defaults to ``has-max-forms``.
``animateForms``:
Whether to animate form addition/deletion.
Defaults to ``false``.
Example
-------
A minimal example project is provided in the ``example/`` directory.
Read ``example/README`` for more information