Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.7 KiB
50 lines
1.7 KiB
5 years ago
|
// Credit https://medium.com/all-about-django/adding-forms-dynamically-to-a-django-formset-375f1090c2b0
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Credit https://medium.com/all-about-django/adding-forms-dynamically-to-a-django-formset-375f1090c2b0
|
||
|
function deleteForm(prefix, btn) {
|
||
|
var total = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
|
||
|
if (total >= 1){
|
||
|
btn.closest('div').remove();
|
||
|
var forms = $('.attachment');
|
||
|
$('#id_' + prefix + '-TOTAL_FORMS').val(forms.length);
|
||
|
for (var i=0, formCount=forms.length; i<formCount; i++) {
|
||
|
$(forms.get(i)).find(':input').each(function() {
|
||
|
updateElementIndex(this, prefix, i);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Credit: https://simpleit.rocks/python/django/dynamic-add-form-with-add-button-in-django-modelformset-template/
|
||
|
$(document).ready(function(){
|
||
|
$('#add_attachment').click(function() {
|
||
|
var form_idx = $('#id_prilohy-TOTAL_FORMS').val();
|
||
|
var new_form = $('#empty_form').html().replace(/__prefix__/g, form_idx);
|
||
|
$('#form_set').append(new_form);
|
||
|
// Newly created form has not the binding between remove button and remove function
|
||
|
// We need to add it manually
|
||
|
$('.remove_attachment').click(function(){
|
||
|
deleteForm("prilohy",this);
|
||
|
});
|
||
|
$('#id_prilohy-TOTAL_FORMS').val(parseInt(form_idx) + 1);
|
||
|
});
|
||
|
$('.remove_attachment').click(function(){
|
||
|
deleteForm("prilohy",this);
|
||
|
});
|
||
|
});
|
||
|
|