Aleksmorshen commited on
Commit
60ad812
·
verified ·
1 Parent(s): 79fed57

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +15 -55
script.js CHANGED
@@ -1,6 +1,7 @@
1
  document.addEventListener('DOMContentLoaded', function () {
2
  const productForm = document.getElementById('productForm');
3
  const productTable = document.getElementById('productTable').getElementsByTagName('tbody')[0];
 
4
  const totalSoldElement = document.getElementById('totalSold');
5
  const totalRevenueElement = document.getElementById('totalRevenue');
6
 
@@ -28,6 +29,19 @@ document.addEventListener('DOMContentLoaded', function () {
28
  }
29
  });
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  // Функция добавления товара
32
  function addProduct(name, purchasePrice, salePrice, quantity) {
33
  const product = {
@@ -49,58 +63,4 @@ document.addEventListener('DOMContentLoaded', function () {
49
 
50
  // Функция добавления товара в таблицу
51
  function addProductToTable(product) {
52
- const row = productTable.insertRow();
53
- row.setAttribute('data-id', product.id);
54
-
55
- row.innerHTML = `
56
- <td>${product.name}</td>
57
- <td>${product.purchasePrice}</td>
58
- <td>${product.salePrice}</td>
59
- <td>${product.quantity}</td>
60
- <td><button class="sell-btn" onclick="sellProduct(${product.id})">Продать</button></td>
61
- `;
62
- }
63
-
64
- // Функция загрузки товаров из localStorage
65
- function loadProducts() {
66
- const products = JSON.parse(localStorage.getItem('products')) || [];
67
- products.forEach(product => addProductToTable(product));
68
- }
69
-
70
- // Функция загрузки статистики из localStorage
71
- function loadStats() {
72
- const stats = JSON.parse(localStorage.getItem('stats')) || { totalSold: 0, totalRevenue: 0 };
73
- totalSold = stats.totalSold;
74
- totalRevenue = stats.totalRevenue;
75
- updateStatsDisplay();
76
- }
77
-
78
- // Функция обновления отображения статистики
79
- function updateStatsDisplay() {
80
- totalSoldElement.textContent = totalSold;
81
- totalRevenueElement.textContent = totalRevenue.toFixed(2);
82
- }
83
-
84
- // Функция продажи товара
85
- window.sellProduct = function (productId) {
86
- let products = JSON.parse(localStorage.getItem('products')) || [];
87
- const productIndex = products.findIndex(p => p.id === productId);
88
-
89
- if (productIndex !== -1 && products[productIndex].quantity > 0) {
90
- products[productIndex].quantity -= 1; // Уменьшаем количество на 1
91
- totalSold += 1; // Увеличиваем общее количество проданных товаров
92
- totalRevenue += products[productIndex].salePrice; // Увеличиваем общую выручку
93
-
94
- // Сохраняем обновленные данные
95
- localStorage.setItem('products', JSON.stringify(products));
96
- localStorage.setItem('stats', JSON.stringify({ totalSold, totalRevenue }));
97
-
98
- // Обновляем таблицу и статистику
99
- productTable.innerHTML = '';
100
- loadProducts();
101
- updateStatsDisplay();
102
- } else {
103
- alert('Товар закончился на складе.');
104
- }
105
- };
106
- });
 
1
  document.addEventListener('DOMContentLoaded', function () {
2
  const productForm = document.getElementById('productForm');
3
  const productTable = document.getElementById('productTable').getElementsByTagName('tbody')[0];
4
+ const searchInput = document.getElementById('searchInput');
5
  const totalSoldElement = document.getElementById('totalSold');
6
  const totalRevenueElement = document.getElementById('totalRevenue');
7
 
 
29
  }
30
  });
31
 
32
+ // Поиск по товарам
33
+ searchInput.addEventListener('input', function () {
34
+ const searchTerm = searchInput.value.toLowerCase();
35
+ const rows = productTable.getElementsByTagName('tr');
36
+
37
+ for (let row of rows) {
38
+ const name = row.getElementsByTagName('td')[0]?.textContent.toLowerCase();
39
+ if (name) {
40
+ row.style.display = name.includes(searchTerm) ? '' : 'none';
41
+ }
42
+ }
43
+ });
44
+
45
  // Функция добавления товара
46
  function addProduct(name, purchasePrice, salePrice, quantity) {
47
  const product = {
 
63
 
64
  // Функция добавления товара в таблицу
65
  function addProductToTable(product) {
66
+ const row