From 3677b8165c650b666e99bd612859a7d4f5d0a3b3 Mon Sep 17 00:00:00 2001 From: keplyx Date: Thu, 1 Aug 2019 00:09:20 +0200 Subject: [PATCH] Added basic item management --- admin/index.php | 66 +++++++++++++++++++++++++++ assets/css/stock.css | 25 +++++++++++ assets/js/stock.js | 102 ++++++++++++++++++++++++++++++++++++++++++ includes/navbar.php | 2 +- includes/sidenav.php | 2 +- includes/template.php | 18 ++++---- index.php | 14 ++++-- 7 files changed, 215 insertions(+), 14 deletions(-) create mode 100644 admin/index.php create mode 100644 assets/css/stock.css create mode 100644 assets/js/stock.js diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..3f24e6e --- /dev/null +++ b/admin/index.php @@ -0,0 +1,66 @@ + +
+ +

Gestion des Stocks

+

Ajouter un article

+ + + + + + + + + + + + + + + + + + + + +
NomQuantitéPrixCode BarreImageActions
+ + + + + + + + + + + +
+ +

Liste d'articles

+ + + + + + + + + + + +
NomQuantitéPrixCode BarreImageActions
+
+ + + + +"; +include($relativePath . "includes/template.php"); +?> diff --git a/assets/css/stock.css b/assets/css/stock.css new file mode 100644 index 0000000..a535a22 --- /dev/null +++ b/assets/css/stock.css @@ -0,0 +1,25 @@ +table { + width: 100%; + margin-bottom: 20px; +} + +.name-column { + width : 30%; +} + +.code-column { + width: 20%; +} + +.stock-container { + width: 90%; + margin: 50px auto auto auto; +} + +.actions-column { + width: 100px; +} + +.image-column { + width: 150px; +} diff --git a/assets/js/stock.js b/assets/js/stock.js new file mode 100644 index 0000000..bfacfb2 --- /dev/null +++ b/assets/js/stock.js @@ -0,0 +1,102 @@ +let defaultStock = [ + { + name: 'Granola', + quantity: '2', + price: '1.3', + code: '7622210601988', + type: [ + 'Alimentaire', + ] + } +]; + +let currentDataset = []; + +$(document).ready(function () { + currentDataset = defaultStock; + generateTable(currentDataset); +}); + + +function generateTable(dataset) { + for (let i = 0; i < dataset.length; i++) { + generateLine(dataset[i]); + } +} + + +function generateLine(item) { + $.selector_cache('#stockTable').append( + '' + + '' + item.name + '' + + '' + item.quantity + '' + + '' + item.price + '' + + '' + item.code + '' + + 'LINK' + + '' + + '' + + '' + + '' + + '' + ); +} + +function getItemOfCode(code) { + let item = {}; + for (let i = 0; i < currentDataset.length; i++) { + if (currentDataset[i].code === code) { + item = currentDataset[i]; + break; + } + } + return item; +} + + +function editItem(elem) { + if (isItemInputEmpty()) { + let code = elem.id.replace('edit_', ''); + let item = getItemOfCode(code); + console.log(item); + setEditFieldValues(item.name, item.quantity, item.price, item.code); + removeItemFromList(item); // Move the item in the edit fields + } +} + +function deleteItem(elem) { + let code = elem.id.replace('delete_', ''); + let item = getItemOfCode(code); + console.log(item); + removeItemFromList(item); +} + +function removeItemFromList(item) { + currentDataset.splice(currentDataset.indexOf(item), 1); + $('#row_' + item.code).remove(); +} + +function setEditFieldValues(name, quantity, price, code) { + $.selector_cache('#nameInput').val(name); + $.selector_cache('#quantityInput').val(quantity); + $.selector_cache('#priceInput').val(price); + $.selector_cache('#codeInput').val(code); +} + +function isItemInputEmpty() { + return $.selector_cache('#nameInput').val() === '' && + $.selector_cache('#quantityInput').val() === '' && + $.selector_cache('#priceInput').val() === '' && + $.selector_cache('#codeInput').val() === ''; +} + +function addNewItem() { + let item = { + name: $.selector_cache('#nameInput').val(), + quantity: $.selector_cache('#quantityInput').val(), + price: $.selector_cache('#priceInput').val(), + code: $.selector_cache('#codeInput').val(), + }; + setEditFieldValues('', '', '', ''); + currentDataset.push(item); + generateLine(item); +} diff --git a/includes/navbar.php b/includes/navbar.php index 291216e..766f7a8 100644 --- a/includes/navbar.php +++ b/includes/navbar.php @@ -43,7 +43,7 @@