Actualiser test.html
CI / PHP Syntax Check (push) Has been cancelled
CI / JavaScript Lint (push) Has been cancelled
CI / Docker Build Test (push) Has been cancelled
CI / Validate Translation Files (push) Has been cancelled
CI / Auto-merge develop → main (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / PHP Syntax Check (push) Has been cancelled
CI / JavaScript Lint (push) Has been cancelled
CI / Docker Build Test (push) Has been cancelled
CI / Validate Translation Files (push) Has been cancelled
CI / Auto-merge develop → main (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
This commit is contained in:
@@ -1,39 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<select id="sel" onchange="fill()"></select>
|
||||
<input id="id" readonly>
|
||||
<input id="cat" placeholder="Catégorie (ex: fruits)">
|
||||
<select id="sel" onchange="fill()">
|
||||
<option value="">-- Choisir un produit --</option>
|
||||
</select>
|
||||
<input id="id" readonly placeholder="ID">
|
||||
<input id="cat" placeholder="Catégorie">
|
||||
<button onclick="save()">SAVE</button>
|
||||
|
||||
<script>
|
||||
// Charger la liste
|
||||
// Variable pour stocker les données complètes des produits
|
||||
let fullProductList = [];
|
||||
|
||||
fetch('api/index.php?action=products_list')
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
const list = data.inventory || data.products || data;
|
||||
list.forEach(p => {
|
||||
fullProductList = data.inventory || data.products || data;
|
||||
const sel = document.getElementById('sel');
|
||||
fullProductList.forEach((p, index) => {
|
||||
let opt = document.createElement('option');
|
||||
opt.value = p.id; opt.textContent = p.name;
|
||||
opt.dataset.cat = p.category;
|
||||
document.getElementById('sel').appendChild(opt);
|
||||
opt.value = index; // On utilise l'index du tableau
|
||||
opt.textContent = p.name;
|
||||
sel.appendChild(opt);
|
||||
});
|
||||
});
|
||||
|
||||
function fill() {
|
||||
let s = document.getElementById('sel');
|
||||
document.getElementById('id').value = s.value;
|
||||
document.getElementById('cat').value = s.options[s.selectedIndex].dataset.cat;
|
||||
let p = fullProductList[s.value];
|
||||
document.getElementById('id').value = p.id;
|
||||
document.getElementById('cat').value = p.category || "";
|
||||
}
|
||||
|
||||
function save() {
|
||||
let s = document.getElementById('sel');
|
||||
let p = fullProductList[s.value]; // On récupère l'objet complet
|
||||
|
||||
// On met à jour uniquement la catégorie dans l'objet original
|
||||
p.category = document.getElementById('cat').value;
|
||||
|
||||
fetch('api/index.php?action=product_save', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
id: document.getElementById('id').value,
|
||||
category: document.getElementById('cat').value
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(p) // On renvoie TOUT l'objet produit
|
||||
})
|
||||
}).then(r => r.json()).then(res => alert(JSON.stringify(res)));
|
||||
.then(r => r.json())
|
||||
.then(res => alert("Réponse serveur : " + JSON.stringify(res)));
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user