Skip to content

Commit 6252301

Browse files
committed
[soc2009/admin-ui] First steps to dynamically add stacked inlines. Upon loading the change list page, all extra inline fields are hidden and an "Add" link is shown at the bottom.
Currently, clicking on the link will only re-show the hidden extra inlines; it won't add any new ones. Proper addition is going to be in the next check-in. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/admin-ui@10939 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent d3e2863 commit 6252301

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

django/contrib/admin/media/css/forms.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,6 @@ fieldset.monospace textarea {
325325
padding-left: 14px;
326326
}
327327

328+
.add_inline {
329+
display: none;
330+
}

django/contrib/admin/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ def change_view(self, request, object_id, extra_context=None):
868868
'root_path': self.admin_site.root_path,
869869
'app_label': opts.app_label,
870870
}
871+
#import ipdb; ipdb.set_trace()
871872
context.update(extra_context or {})
872873
return self.render_change_form(request, context, change=True, obj=obj)
873874
change_view = transaction.commit_on_success(change_view)
@@ -1122,7 +1123,7 @@ def __init__(self, parent_model, admin_site):
11221123

11231124
def _media(self):
11241125
from django.conf import settings
1125-
js = []
1126+
js = ['js/jquery.js']
11261127
if self.prepopulated_fields:
11271128
js.append('js/urlify.js')
11281129
if self.filter_vertical or self.filter_horizontal:

django/contrib/admin/templates/admin/edit_inline/stacked.html

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
55
{{ inline_admin_formset.formset.non_form_errors }}
66

77
{% for inline_admin_form in inline_admin_formset %}
8-
<div class="inline-related{% if forloop.last %} last-related{% endif %}">
8+
<div class="inline-related{% if forloop.last %} last-related{% endif %}" id="{{ inline_admin_formset.opts.verbose_name}}{{ forloop.counter }}">
99
<h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;{% if inline_admin_form.original %}{{ inline_admin_form.original }}{% else %} #{{ forloop.counter }}{% endif %}
1010
{% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %}
1111
</h3>
@@ -22,7 +22,33 @@ <h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;{% if inline
2222
</div>
2323
{% endfor %}
2424

25-
{# <ul class="tools"> #}
26-
{# <li><a class="add" href="">Add another {{ inline_admin_formset.opts.verbose_name|title }}</a></li> #}
27-
{# </ul> #}
25+
<ul class="tools add_inline">
26+
<li><a id="{{ inline_admin_formset.opts.verbose_name }}-add" class="add" href="#">Add a {{ inline_admin_formset.opts.verbose_name }}</a></li>
27+
</ul>
28+
2829
</div>
30+
31+
<script type="text/javascript">
32+
$(function() {
33+
{# TODO Zain: properly set the TOTAL_FORMS hidden input field #}
34+
35+
var id_prefix = "{{ inline_admin_formset.opts.verbose_name }}";
36+
var total_forms = {{ inline_admin_formset.formset.management_form.initial.TOTAL_FORMS }};
37+
var initial_forms = {{ inline_admin_formset.formset.management_form.initial.INITIAL_FORMS }};
38+
39+
// since javascript is turned on, unhide the "add new <inline>" link and hide the extras
40+
$('.add_inline').show();
41+
42+
for(var i = initial_forms + 1; i <= total_forms; i++) {
43+
$('#' + id_prefix + i).hide();
44+
}
45+
46+
total_forms = initial_forms;
47+
48+
// clicking on the "add" link will add a blank form to add a new inline object
49+
$('#' + id_prefix + "-add").click(function() {
50+
total_forms++;
51+
$('#' + id_prefix + (total_forms)).show();
52+
});
53+
});
54+
</script>

0 commit comments

Comments
 (0)