changed basket to a table

This commit is contained in:
bronku 2025-04-06 22:37:02 +02:00
parent 8ec446ea28
commit 923b575bc1
4 changed files with 34 additions and 45 deletions

View file

@ -1,33 +1,30 @@
function addCake(id, name, amount) { function addCake(id, name, price, amount) {
let template = document.getElementById("basket_element_template"); const cake = document.querySelector(`[name="cake[${id}]"]`);
let tr = template.content.cloneNode(true);
console.log(tr);
const cake = document.getElementById("cake[" + id + "]");
if (cake != null) { if (cake != null) {
const value = Number(cake.getAttribute("value")); cake.value = Number(cake.value) + 1;
cake.setAttribute("value", value + 1);
return; return;
} }
const label = document.createElement("label"); let template = document.getElementById("basket_element_template");
label.innerText = name; let tr = template.content.querySelector("tr").cloneNode(true);
const input = document.createElement("input"); tr.querySelector(".cake-name").innerText = name;
input.setAttribute("id", "cake[" + id + "]"); tr.querySelector(".cake-price").innerText = `${price}PLN`;
input.setAttribute("type", "number"); tr.querySelector(".cake-total").innerText = `${price * amount}PLN`;
input.setAttribute("min", "0"); tr.querySelector("button").onclick = () => {
input.setAttribute("name", "cake[" + id + "]"); tr.remove();
input.setAttribute("value", amount); };
const button = document.createElement("button"); let input = tr.querySelector("input");
button.setAttribute("type", "button"); input.onchange = () => {
button.setAttribute("onClick", "removeCake(" + id + ")"); tr.querySelector(".cake-total").innerText = `${price * input.value}PLN`;
button.innerText = "usuń"; };
const li = document.createElement("li"); input.value = amount;
li.appendChild(label); input.name = `cake[${id}]`;
li.appendChild(input); document.querySelector("#basket_table").appendChild(tr);
li.appendChild(button);
li.setAttribute("id", "li[" + id + "]");
const ul = document.getElementById("basket");
ul.appendChild(li);
} }
function removeCake(id) { function toggleDetails(row) {
document.getElementById("li[" + id + "]").remove(); const detailsRow = row.nextElementSibling;
if (detailsRow.classList.contains("hidden")) {
detailsRow.classList.remove("hidden");
return;
}
detailsRow.classList.add("hidden");
} }

View file

@ -6,7 +6,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>{{template "title" .}}</title> <title>{{template "title" .}}</title>
<script src="/static/htmx2.04.js" defer></script> <script src="/static/htmx2.04.js" defer></script>
<script src="/static/order.js" defer></script> <script src="/static/order.js" ></script>
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css">
<link rel="stylesheet" href="/static/fontawesome/css/fontawesome.min.css"> <link rel="stylesheet" href="/static/fontawesome/css/fontawesome.min.css">
<link rel="stylesheet" href="/static/fontawesome/css/solid.css"> <link rel="stylesheet" href="/static/fontawesome/css/solid.css">

View file

@ -62,10 +62,10 @@
<table> <table>
{{range .}} {{range .}}
<tr> <tr>
<th>{{.Name}}<br><small>{{.Category}}</small></th> <th>{{.Name}}<br><small>{{.Category}} (#{{.ID}})</small></th>
<th>{{.Price}}PLN</th> <th>{{.Price}}PLN</th>
<th> <th>
<button type=button onClick="addCake({{.ID}},'{{.Name}} ({{.Price}}PLN)', 1)"> <button type=button onClick="addCake({{.ID}},{{.Name}}, {{.Price}}, 1)">
<i class="fa-solid fa-plus"></i> <i class="fa-solid fa-plus"></i>
</button> </button>
</th> </th>
@ -79,14 +79,16 @@
</main> </main>
<template id="basket_element_template"> <template id="basket_element_template">
<tr> <tr>
<th>Name</th> <th class="cake-name">Name</th>
<th class="cake-price">Price</th>
<th> <th>
<label> <label>
<input type="text"> <input type="number" min="0">
</label> </label>
</th> </th>
<th class="cake-total">Total</th>
<th> <th>
<button type=button onClick=""> <button type=button>
<i class="fa-solid fa-minus"></i> <i class="fa-solid fa-minus"></i>
</button> </button>
</th> </th>
@ -94,7 +96,7 @@
</template> </template>
<script> <script>
{{range .Order.Cakes}} {{range .Order.Cakes}}
addCake({{.ID}},'{{.Name}} ({{.Price}}PLN)', {{.Amount}}) addCake({{.ID}},{{.Name}}, {{.Price}}, {{.Amount}})
{{end}} {{end}}
</script> </script>
{{end}} {{end}}

View file

@ -28,16 +28,6 @@ Zamówienia
background-color: #e6f2ff; background-color: #e6f2ff;
} }
</style> </style>
<script>
function toggleDetails(row) {
const detailsRow=row.nextElementSibling;
if(detailsRow.classList.contains('hidden')) {
detailsRow.classList.remove('hidden');
return;
}
detailsRow.classList.add('hidden');
}
</script>
{{end}} {{end}}
{{define "orders-table"}} {{define "orders-table"}}
<table id="orders_table"> <table id="orders_table">