From bb044e6d2b80c838b1c083114e17729f5537e4a2 Mon Sep 17 00:00:00 2001 From: Florian Lentsch Date: Fri, 2 Aug 2024 10:12:16 +0200 Subject: [PATCH] Fixes #86 --- app/assets/javascripts/article-form.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/article-form.js b/app/assets/javascripts/article-form.js index 205362aa..b4703999 100644 --- a/app/assets/javascripts/article-form.js +++ b/app/assets/javascripts/article-form.js @@ -466,9 +466,13 @@ class ArticleForm { } prepareRatioDataForSequentialRepresentation() { - const numberOfRatios = $(`input[name^="${this.unitFieldsNamePrefix}[article_unit_ratios_attributes]"][name$="[quantity]"]`).length; + const indices = $(`input[name^="${this.unitFieldsNamePrefix}[article_unit_ratios_attributes]"][name$="[quantity]"]`) + .toArray() + .map((field) => parseInt(field.name.replace(/.+\[([0-9]+)\]\[quantity\]/, '$1'))); + const maxIndex = Math.max(...indices); + const minIndex = Math.min(...indices); - for (let i = numberOfRatios; i > 1; i--) { + for (let i = maxIndex; i > minIndex; i--) { const currentField$ = $(`input[name="${this.ratioQuantityFieldNameByIndex(i)}"]`, this.articleForm$); const currentValue = currentField$.val(); const previousValue = $(`input[name="${this.ratioQuantityFieldNameByIndex(i - 1)}"]:last`, this.articleForm$).val();