Skip to content

Commit

Permalink
changed callbacks to jquery events for simpler interfacing with forms…
Browse files Browse the repository at this point in the history
…et adding and removing
  • Loading branch information
bendavis78 committed Jul 9, 2011
1 parent 5e27e7e commit 75e0679
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 8 additions & 8 deletions formhelper/static/formhelper/js/formsets.js
Expand Up @@ -72,10 +72,10 @@
// Remove the parent form containing this button:
var row = $(this).parents("." + options.formCssClass);
row.remove();
// If a post-delete callback was provided, call it with the deleted form:
if (options.removed) {
options.removed(row);
}

// triger remove event
row.trigger('formsetremove');

// Update the TOTAL_FORMS form count.
var forms = $("." + options.formCssClass);
$("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length-1);
Expand Down Expand Up @@ -153,10 +153,10 @@
}
updateDeleteLinks(row);
updateAddAnotherText(row);
// If a post-add callback was supplied, call it with the added form:
if (options.added) {
options.added(row);
}

// trigger formset added event
row.trigger('formsetadd');

return false;
});
updateDeleteLinks($(this));
Expand Down
15 changes: 10 additions & 5 deletions formhelper/templates/formhelper/includes/formset.html
Expand Up @@ -11,18 +11,20 @@
</div>
{% endfor %}
</div>
{# TODO: There may be a more extensible way to do this js #}
<script type="text/javascript" src="{{ STATIC_URL }}formhelper/js/formsets.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var prefix = '{{ formset.prefix }}';
var verbose_name = '{{ formset.verbose_name|lower }}';
var rows = '.dynamic-form.{{ formset.prefix }}';
var updateInlineLabel = function(row) {
$(rows).find("legend span.counter").each(function(i) {
var updateInlineLabel = function(e) {
$(this).parent().find('.dynamic-form').find("legend span.counter").each(function(i) {
var count = i + 1;
$(this).html($(this).html().replace(/#(\d+)/g, '#'+count));
});
}
// use proper grammar
var a = verbose_name.match(/^[AEIOUaeiou]/) ? 'an' : 'a';
$(rows).formset({
prefix: prefix,
Expand All @@ -31,9 +33,12 @@
addTitle: "Click here to add another"+verbose_name,
addTitleInitial: "Click here to add "+a+" "+verbose_name,
deleteText: "Remove",
removed: updateInlineLabel,
added: updateInlineLabel
});
updateInlineLabel();
$('.dynamic-form').bind('formsetadd', updateInlineLabel);
$('.dynamic-form').bind('formsetremove', updateInlineLabel);
// make sure forms that are already displayed are updated
$('.dynamic-form').not('.empty-form').each(function(){
updateInlineLabel(null);
});
});
</script>

0 comments on commit 75e0679

Please sign in to comment.