Created
March 20, 2012 03:08
-
-
Save jteso/2130643 to your computer and use it in GitHub Desktop.
Dynamically adding forms to a formset with jQuery in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateElementIndex(el, prefix, ndx) { | |
var id_regex = new RegExp('(' + prefix + '-\\d+)'); | |
var replacement = prefix + '-' + ndx; | |
if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement)); | |
if (el.id) el.id = el.id.replace(id_regex, replacement); | |
if (el.name) el.name = el.name.replace(id_regex, replacement); | |
} | |
function addForm(btn, prefix) { | |
var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val()); | |
var row = $('.dynamic-form:first').clone(true).get(0); | |
$(row).removeAttr('id').insertAfter($('.dynamic-form:last')).children('.hidden').removeClass('hidden'); | |
$(row).children().not(':last').children().each(function() { | |
updateElementIndex(this, prefix, formCount); | |
$(this).val(''); | |
}); | |
$(row).find('.delete-row').click(function() { | |
deleteForm(this, prefix); | |
}); | |
$('#id_' + prefix + '-TOTAL_FORMS').val(formCount + 1); | |
return false; | |
} | |
function deleteForm(btn, prefix) { | |
$(btn).parents('.dynamic-form').remove(); | |
var forms = $('.dynamic-form'); | |
$('#id_' + prefix + '-TOTAL_FORMS').val(forms.length); | |
for (var i=0, formCount=forms.length; i<formCount; i++) { | |
$(forms.get(i)).children().not(':last').children().each(function() { | |
updateElementIndex(this, prefix, i); | |
}); | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
<!-- | |
$(function () { | |
$('.add-row').click(function() { | |
return addForm(this, 'form'); | |
}); | |
$('.delete-row').click(function() { | |
return deleteForm(this, 'form'); | |
}) | |
}) | |
//--> | |
</script> | |
<table id="id_forms_table" border="0" cellpadding="0" cellspacing="5"> | |
<thead> | |
<tr> | |
<th scope="col"> </th> | |
<th scope="col">Property name</th> | |
<th scope="col"> </th> | |
<th scope="col"> </th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for form in property_formset.forms %} | |
<tr id="{{ form.prefix }}-row" class="dynamic-form"> | |
<td{% if forloop.first %} class="hidden"{% endif %}>{{ form.operand }}</td> | |
<td>{{ form.property }}</td> | |
<td>contains {{ form.value }}</td> | |
<td{% if forloop.first %} class="hidden"{% endif %}> | |
<a id="remove-{{ form.prefix }}-row" href="javascript:void(0)" class="delete-row"></a> | |
</td> | |
</tr> | |
{% endfor %} | |
<tr> | |
<td colspan="4"><a href="javascript:void(0)" class="add-row">add property</a></td> | |
</tr> | |
</tbody> | |
</table> | |
{{ property_form.management_form }} | |
<div> | |
<input type="submit" value=" Find » " /> | |
</div> |
This url explains how to have a nested formset within a form:
http://stackoverflow.com/questions/5518826/how-to-have-a-nested-inline-formset-within-a-form-in-django
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternatively (found in Stackoverflow):
My template:
My Services
{{ serviceFormset.management_form }}
{% for form in serviceFormset.forms %}
{{ form.as_table }}
{% endfor %}
<script> $('#add_more').click(function() { cloneMore('div.table:last', 'service'); }); </script>
In a javascript file:
function cloneMore(selector, type) {
var newElement = $(selector).clone(true);
var total = $('#id_' + type + '-TOTAL_FORMS').val();
newElement.find(':input').each(function() {
var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-');
var id = 'id_' + name;
$(this).attr({'name': name, 'id': id}).val('').removeAttr('checked');
});
newElement.find('label').each(function() {
var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-');
$(this).attr('for', newFor);
});
total++;
$('#id_' + type + '-TOTAL_FORMS').val(total);
$(selector).after(newElement);
}