diff options
author | Daniel García Moreno <danigm@soleta.eu> | 2021-08-04 18:50:26 +0200 |
---|---|---|
committer | Javier Sánchez Parra <jsanchez@soleta.eu> | 2021-08-23 17:16:04 +0200 |
commit | 0bbae508f60b86cb77c3f05e43d4e86bb5d2980d (patch) | |
tree | 28b392172b8c73f4cdd63bfd6be83c9c2c55e9c8 /ogcp | |
parent | a51fdd9d30bbd257a00f7f9e167babbfbd8669ed (diff) |
Fix setup form javascript
This patch fixes the javascript that adds/removes rows from the setup
form.
The AddPartition function now replaces the id `partitions-N-FIELDNAME`
using a regular expression. The hidden csrf input is also ignored to get
incremental numbers.
On deletion, all the ids are reassign from zero, in order, to get always
an ordered list with all numbers between the first and the last.
Diffstat (limited to 'ogcp')
-rw-r--r-- | ogcp/static/js/ogcp.js | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/ogcp/static/js/ogcp.js b/ogcp/static/js/ogcp.js index 00ab61c..8a538ca 100644 --- a/ogcp/static/js/ogcp.js +++ b/ogcp/static/js/ogcp.js @@ -58,13 +58,11 @@ function AddPartition(evt) { const target = $($(evt).data("target")); const oldrow = target.find("[data-toggle=fieldset-entry]:last"); const row = oldrow.clone(true, true); - const elem_id = row.find(":input")[0].id; - const elem_prefix = elem_id.replace(/(.*)-(\d{1,4})/m, '$1')// max 4 digits for ids in list + const elem_id = row.find(".form-control")[0].id; const elem_num = parseInt(elem_id.replace(/(.*)-(\d{1,4})/m, '$2')) + 1; // Increment WTForms unique identifiers - row.children(':input').each(function() { - const id = $(this).attr('id').replace(elem_prefix+'-' + (elem_num - 1), - elem_prefix+'-' + (elem_num)); + row.find('.form-control').each(function() { + const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${elem_num}-$3`); $(this).attr('name', id).attr('id', id).val('').removeAttr("checked"); }); row.show(); @@ -73,5 +71,14 @@ function AddPartition(evt) { function RemovePartition(evt) { const target = $(evt).parent().parent(); + const table = target.parent(); target.remove(); + + // Reassign rows ids + table.find('tr').each(function(index) { + $(this).find('.form-control').each(function() { + const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${index}-$3`); + $(this).attr('name', id).attr('id', id); + }); + }); } |