refactor: string to variable.
This commit is contained in:
parent
b4b6144611
commit
c993225daf
@ -3,12 +3,16 @@ const quantityLines = document.getElementById('id_quantity_lines');
|
||||
const quantityProducts = document.getElementById('id_quantity_products');
|
||||
const ammountInput = document.getElementById('id_ammount');
|
||||
const idPrefix = 'id_saleline_set-';
|
||||
const quantitySuffix = '-quantity';
|
||||
const priceSuffix = '-unit_price';
|
||||
const quantityRegexSelector = `[id^="${idPrefix}"][id$="${quantitySuffix}"]`;
|
||||
const priceRegexSelector = `[id^="${idPrefix}"][id$="${priceSuffix}"]`;
|
||||
|
||||
complete_form.addEventListener('change', function(event){
|
||||
if (event.target.matches('[id^="${idPrefix}"][id$="-quantity"]')) {
|
||||
if (event.target.matches(quantityRegexSelector)) {
|
||||
calculateSummary();
|
||||
}
|
||||
if (event.target.matches('[id^="id_saleline_set-"][id$="-unit_price"]')) {
|
||||
if (event.target.matches(priceRegexSelector)) {
|
||||
calculateSummary();
|
||||
}
|
||||
});
|
||||
@ -17,13 +21,13 @@ complete_form.addEventListener('change', function(event){
|
||||
function calculateSummary() {
|
||||
let quantity = 0;
|
||||
let ammount = 0;
|
||||
const quantityInputs = document.querySelectorAll('[id^="id_saleline_set-"][id$="-quantity"]');
|
||||
const quantityInputs = document.querySelectorAll(quantityRegexSelector);
|
||||
const ids = Array.prototype.map.call(quantityInputs, function(input) {
|
||||
return input.id.match(/\d+/)[0];
|
||||
});
|
||||
ids.forEach(function(id) {
|
||||
let lineQuantity = document.getElementById(`id_saleline_set-${id}-quantity`)
|
||||
let linePrice = document.getElementById(`id_saleline_set-${id}-unit_price`)
|
||||
let lineQuantity = document.getElementById(`${idPrefix}${id}${quantitySuffix}`)
|
||||
let linePrice = document.getElementById(`${idPrefix}${id}${priceSuffix}`)
|
||||
quantity += parseFloat(lineQuantity.value);
|
||||
ammount += parseFloat(linePrice.value) * parseFloat(lineQuantity.value);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user