Merge branch 'Aufinal/transfer_formset' into 'master'

Fix : nouveaux formulaires de transfert si le formset est plein

Closes #250

See merge request klub-dev-ens/gestioCOF!404
This commit is contained in:
Martin Pepin 2020-01-04 16:49:42 +01:00
commit ee79281f53
2 changed files with 44 additions and 6 deletions

View file

@ -11,6 +11,8 @@ Liste des changements notables dans GestioCOF depuis la version 0.1 (septembre
## Upcoming
- La page des transferts permet de créer un nombre illimité de transferts en
une fois.
- Nouveau site du COF : les liens sont optionnels dans les descriptions de clubs
- Mise à jour du lien vers le calendire de la K-Fêt sur la page d'accueil
- Certaines opérations sont à nouveau accessibles depuis la session partagée

View file

@ -20,7 +20,7 @@
--><button type="submit" id="submit" class="btn btn-primary btn-lg">Enregistrer</button>
{{ transfer_formset.management_form }}
</div>
<table class="transfer_formset table">
<table class="transfer_formset table" id="transfer_formset">
<thead>
<tr>
<td></td>
@ -52,6 +52,7 @@
<script type="text/javascript">
$(document).ready(function () {
function getAccountData(trigramme, callback = function() {}) {
$.ajax({
dataType: "json",
@ -75,7 +76,12 @@ $(document).ready(function () {
getAccountData(trigramme, function(data) {
$input_id.val(data.id);
$data.text(data.name);
$next.focus();
if (!$next.length) {
new_row = add_form();
new_row.find(".from_acc input").focus();
} else {
$next.focus();
}
});
} else {
$input_id.val('');
@ -83,9 +89,10 @@ $(document).ready(function () {
}
}
$('.input_from_acc, .input_to_acc').on('input', function() {
var tri = $(this).val().toUpperCase();
updateAccountData(tri, $(this));
$(document).on("input", '.input_from_acc, .input_to_acc', function(e) {
var target = $(e.target)
var tri = target.val().toUpperCase();
updateAccountData(tri, target);
});
$('#transfers_form').on('submit', function(e) {
@ -101,7 +108,6 @@ $(document).ready(function () {
method : "POST",
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
@ -118,6 +124,36 @@ $(document).ready(function () {
}
});
}
function add_form() {
var row_template = `
<tr class="transfer_form" id="{{ transfer_formset.empty_form.prefix }}">
<td class="from_acc_data"></td>
<td class="from_acc">
<input type="text" name="from_acc" class="input_from_acc" maxlength="3" autocomplete="off" spellcheck="false">
{{ transfer_formset.empty_form.from_acc }}
</td>
<td class="amount">{{ transfer_formset.empty_form.amount }}</td>
<td class="to_acc">
<input type="text" name="to_acc" class="input_to_acc" maxlength="3" autocomplete="off" spellcheck="false">
{{ transfer_formset.empty_form.to_acc }}
</td>
<td class="to_acc_data"></td>
</tr>`;
last_row = $("#transfer_formset tr:last")
// L'id de chaque form ressemble à `form-<id>`
num_rows = parseInt(last_row.attr("id").split("-")[1]);
new_row = $(row_template.replace(/__prefix__/g, num_rows + 1))
last_row.after(new_row)
// On incrémente le nombre de formsets dans la form globale
$("#id_form-TOTAL_FORMS").val(function(i, oldval) {
return ++oldval ;
});
return new_row
}
});
</script>