Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified django_inline_formset/db.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added django_inline_formset/images/Asset_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added django_inline_formset/images/quotes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added django_inline_formset/images/quotes.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions django_inline_formset/products/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class Meta:

VariantFormSet = inlineformset_factory(
Product, Variant, form=VariantForm,
extra=1, can_delete=True,
extra=0, can_delete=True,
can_delete_extra=True
)
ImageFormSet = inlineformset_factory(
Product, Image, form=ImageForm,
extra=1, can_delete=True,
extra=0, can_delete=True,
can_delete_extra=True
)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ <h4 class="card-title">Add Products</h4>
{% with named_formsets.images as formset %}
{{ formset.management_form }}
<script type="text/html" id="images-template"> // id="inlineformsetname-template"
<tr id="images-__prefix__" class= hide_all> // id="inlineformsetname-__prefix__"
<tr id="images-__prefix__">
{% for fields in formset.empty_form.hidden_fields %}
{{ fields }}
{% endfor %}

{% for fields in formset.empty_form.visible_fields %}
<td>{{fields}}</td>
{% endfor %}
<td>
<button type="button" class="btn btn-danger remove-image">Remove</button>
</td>
</tr>
</script>

Expand Down Expand Up @@ -98,6 +101,10 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
</div>
</div>
</div>
</td>
{% else %}
<td>
<button type="button" class="btn btn-danger remove-image">Remove</button>
</td>
{% endif %}
</tr>
Expand All @@ -116,16 +123,18 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
<!-- Note: here variants is our VariantFormSet name, used in get_named_formsets function in views.py -->
{% with named_formsets.variants as formset %}
{{ formset.management_form }}
<script type="text/html" id="variants-template"> // id="inlineformsetname-template"
// id='inlineformsetname-__prefix__'
<tr id="variants-__prefix__" class= hide_all>
<script type="text/html" id="variants-template">
<tr id="variants-__prefix__">
{% for fields in formset.empty_form.hidden_fields %}
{{ fields }}
{% endfor %}

{% for fields in formset.empty_form.visible_fields %}
<td>{{fields}}</td>
{% endfor %}
<td>
<button type="button" class="btn btn-danger remove-variant">Remove</button>
</td>
</tr>
</script>
<div class="table-responsive card mt-4">
Expand All @@ -147,7 +156,7 @@ <h4 class="card-title">Add Variants</h4>
{% endfor %}
{% for formss in formset %}
{{ formss.management_form }}
<tr id="variants-{{ forloop.counter0 }}" class= hide_all> <!-- id="inlineformsetname-counter" -->
<tr id="variants-{{ forloop.counter0 }}"> <!-- id="inlineformsetname-counter" -->
{{ formss.id }}
{% for field in formss.visible_fields %}
<td>
Expand Down Expand Up @@ -182,6 +191,10 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
</div>
</div>
</div>
</td>
{% else %}
<td>
<button type="button" class="btn btn-danger remove-variant">Remove</button>
</td>
{% endif %}
</tr>
Expand All @@ -192,7 +205,7 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
</div>

{% endwith %}
<!-- inline form for Images end -->
<!-- inline form for Variant end -->

<div class="form-group">
<button type="submit" class="btn btn-secondary btn-block">Submit</button>
Expand All @@ -214,6 +227,15 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
// update form count
$('#id_images-TOTAL_FORMS').attr('value', count+1);
});
// when user clicks remove btn of images
$('#item-images').on('click', '.remove-image', function(ev) {
ev.preventDefault();
$(this).closest('tr').remove();

// update form count
var count = $('#item-images').children().length;
$('#id_images-TOTAL_FORMS').attr('value', count);
});
});

$(document).ready(function() {
Expand All @@ -228,6 +250,15 @@ <h5 class="modal-title" id="exampleModalLabel{{formss.instance.pk}}">Are Your Su
// update form count
$('#id_variants-TOTAL_FORMS').attr('value', count+1);
});
// when user clicks remove btn of variants
$('#item-variants').on('click', '.remove-variant', function(ev) {
ev.preventDefault();
$(this).closest('tr').remove();

// update form count
var count = $('#item-variants').children().length;
$('#id_variants-TOTAL_FORMS').attr('value', count);
});
});
</script>

Expand Down