Feature: таблица товаров без скролла, товар обрезается, цена/стоимость с разделителем разрядов
Made-with: Cursor
This commit is contained in:
@@ -55,10 +55,10 @@
|
||||
<table class="ws-table ws-table-items" id="supplier-order-items">
|
||||
<colgroup>
|
||||
<col class="ws-col-product">
|
||||
<col>
|
||||
<col>
|
||||
<col class="ws-col-price">
|
||||
<col class="ws-col-currency">
|
||||
<col class="ws-col-qty">
|
||||
<col>
|
||||
<col class="ws-col-cost">
|
||||
<col class="ws-col-del">
|
||||
</colgroup>
|
||||
<thead>
|
||||
@@ -74,11 +74,11 @@
|
||||
<tbody id="supplier-order-items-body">
|
||||
{% for f in formset %}
|
||||
<tr class="item-row">
|
||||
<td>{{ f.id }}{{ f.product }}</td>
|
||||
<td>{{ f.price }}</td>
|
||||
<td>{{ f.currency }}</td>
|
||||
<td class="ws-col-product">{{ f.id }}{{ f.product }}</td>
|
||||
<td class="ws-col-price">{{ f.price }}</td>
|
||||
<td class="ws-col-currency">{{ f.currency }}</td>
|
||||
<td class="ws-col-qty">{{ f.quantity }}</td>
|
||||
<td class="row-amount ws-num">—</td>
|
||||
<td class="row-amount ws-col-cost ws-num">—</td>
|
||||
<td>{% if f.DELETE %}{{ f.DELETE }}{% endif %}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
@@ -107,17 +107,52 @@
|
||||
return name.replace(/-TOTAL_FORMS$/, '');
|
||||
}
|
||||
|
||||
function formatNum(x) {
|
||||
var n = parseFloat(x);
|
||||
if (isNaN(n)) return '—';
|
||||
var parts = n.toFixed(2).split('.');
|
||||
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, '\u202f');
|
||||
return parts.join(',');
|
||||
}
|
||||
function parseNum(s) {
|
||||
return parseFloat(String(s).replace(/\s/g, '').replace(',', '.')) || 0;
|
||||
}
|
||||
|
||||
function updateRowAmounts() {
|
||||
document.querySelectorAll('#supplier-order-items .item-row').forEach(function(row) {
|
||||
var price = parseFloat(row.querySelector('input[name$="-price"]')?.value) || 0;
|
||||
var qty = parseFloat(row.querySelector('input[name$="-quantity"]')?.value) || 0;
|
||||
var priceInput = row.querySelector('input[name$="-price"]');
|
||||
var qty = parseNum(row.querySelector('input[name$="-quantity"]')?.value);
|
||||
var price = parseNum(priceInput?.value);
|
||||
var el = row.querySelector('.row-amount');
|
||||
if (el) el.textContent = (price * qty).toFixed(2);
|
||||
if (el) el.textContent = formatNum(price * qty);
|
||||
});
|
||||
}
|
||||
|
||||
form.addEventListener('input', updateRowAmounts);
|
||||
|
||||
form.querySelectorAll('#supplier-order-items input[name$="-price"]').forEach(function(input) {
|
||||
input.addEventListener('blur', function() {
|
||||
var v = parseNum(this.value);
|
||||
if (!isNaN(v) && this.value.trim() !== '') this.value = formatNum(v).replace(',', '.');
|
||||
});
|
||||
input.addEventListener('focus', function() {
|
||||
this.value = String(this.value).replace(/\s/g, '').replace(',', '.');
|
||||
});
|
||||
});
|
||||
form.addEventListener('submit', function() {
|
||||
form.querySelectorAll('input[name$="-price"]').forEach(function(input) {
|
||||
input.value = String(input.value).replace(/\s/g, '').replace(',', '.');
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelectorAll('#supplier-order-items input[name$="-price"]').forEach(function(input) {
|
||||
if (input.value && input.value.trim() !== '') {
|
||||
var v = parseNum(input.value);
|
||||
input.value = formatNum(v).replace(',', '.');
|
||||
}
|
||||
});
|
||||
updateRowAmounts();
|
||||
|
||||
document.getElementById('add-supplier-order-row').addEventListener('click', function() {
|
||||
var rows = tbody.querySelectorAll('.item-row');
|
||||
var lastRow = rows[rows.length - 1];
|
||||
@@ -141,6 +176,13 @@
|
||||
});
|
||||
var amountCell = clone.querySelector('.row-amount');
|
||||
if (amountCell) amountCell.textContent = '—';
|
||||
clone.querySelector('input[name$="-price"]')?.addEventListener('blur', function() {
|
||||
var v = parseNum(this.value);
|
||||
if (!isNaN(v) && this.value.trim() !== '') this.value = formatNum(v).replace(',', '.');
|
||||
});
|
||||
clone.querySelector('input[name$="-price"]')?.addEventListener('focus', function() {
|
||||
this.value = String(this.value).replace(/\s/g, '').replace(',', '.');
|
||||
});
|
||||
tbody.appendChild(clone);
|
||||
totalInput.value = nextIndex + 1;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user