| 36 | // add delete buttons to all rows that don't have a delete checkbox |
| 37 | $(this).each(function() { |
| 38 | // skip rows that have an original |
| 39 | if (!$(this).find("[name=" + $(this).attr("id") + "-id]").val()) { |
| 40 | if ($(this).is("tr")) { |
| 41 | // If the forms are laid out in table rows, insert |
| 42 | // the remove button into the last table cell: |
| 43 | $(this).children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>"); |
| 44 | } else if ($(this).is("ul") || $(this).is("ol")) { |
| 45 | // If they're laid out as an ordered/unordered list, |
| 46 | // insert an <li> after the last list item: |
| 47 | $(this).append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>"); |
| 48 | } else { |
| 49 | // Otherwise, just insert the remove button as the |
| 50 | // last child element of the form's container: |
| 51 | $(this).children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>"); |
| 52 | } |
| 53 | // The delete button of each row triggers a bunch of other things |
| 54 | $(this).find("a." + options.deleteCssClass).click(function() { |
| 55 | // Remove the parent form containing this button: |
| 56 | var row = $(this).parents("." + options.formCssClass); |
| 57 | row.remove(); |
| 58 | nextIndex -= 1; |
| 59 | // If a post-delete callback was provided, call it with the deleted form: |
| 60 | if (options.removed) { |
| 61 | options.removed(row); |
| 62 | } |
| 63 | // Update the TOTAL_FORMS form count. |
| 64 | var forms = $("." + options.formCssClass); |
| 65 | $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length); |
| 66 | // Show add button again once we drop below max |
| 67 | if ((maxForms.val() == '') || (maxForms.val()-forms.length) > 0) { |
| 68 | addButton.parent().show(); |
| 69 | } |
| 70 | // Also, update names and ids for all remaining form controls |
| 71 | // so they remain in sequence: |
| 72 | for (var i=0, formCount=forms.length; i<formCount; i++) |
| 73 | { |
| 74 | updateElementIndex($(forms).get(i), options.prefix, i); |
| 75 | $(forms.get(i)).find("*").each(function() { |
| 76 | updateElementIndex(this, options.prefix, i); |
| 77 | }); |
| 78 | } |
| 79 | return false; |
| 80 | }); |
| 81 | } |
| 82 | }); |
62 | | if (row.is("tr")) { |
63 | | // If the forms are laid out in table rows, insert |
64 | | // the remove button into the last table cell: |
65 | | row.children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>"); |
66 | | } else if (row.is("ul") || row.is("ol")) { |
67 | | // If they're laid out as an ordered/unordered list, |
68 | | // insert an <li> after the last list item: |
69 | | row.append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>"); |
70 | | } else { |
71 | | // Otherwise, just insert the remove button as the |
72 | | // last child element of the form's container: |
73 | | row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>"); |
74 | | } |
87 | | // The delete button of each row triggers a bunch of other things |
88 | | row.find("a." + options.deleteCssClass).click(function() { |
89 | | // Remove the parent form containing this button: |
90 | | var row = $(this).parents("." + options.formCssClass); |
91 | | row.remove(); |
92 | | nextIndex -= 1; |
93 | | // If a post-delete callback was provided, call it with the deleted form: |
94 | | if (options.removed) { |
95 | | options.removed(row); |
96 | | } |
97 | | // Update the TOTAL_FORMS form count. |
98 | | var forms = $("." + options.formCssClass); |
99 | | $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length); |
100 | | // Show add button again once we drop below max |
101 | | if ((maxForms.val() == '') || (maxForms.val()-forms.length) > 0) { |
102 | | addButton.parent().show(); |
103 | | } |
104 | | // Also, update names and ids for all remaining form controls |
105 | | // so they remain in sequence: |
106 | | for (var i=0, formCount=forms.length; i<formCount; i++) |
107 | | { |
108 | | updateElementIndex($(forms).get(i), options.prefix, i); |
109 | | $(forms.get(i)).find("*").each(function() { |
110 | | updateElementIndex(this, options.prefix, i); |
111 | | }); |
112 | | } |
113 | | return false; |
114 | | }); |