Commit 99ff03df authored by Mat's avatar Mat

Améliore la clarté du script

parent 1203b7d5
......@@ -62,21 +62,24 @@
let submitURL = null;
// L'URL de l'API
let ownersURL = Routing.generate('api_owner_read');
// Le tableau
let table = document.querySelector("table#table-owners");
let tbody = table.querySelector("tbody");
// La fonction AJAX qui reload la table
/*
* Affichage du tableau (read)
*/
let ownersURL = Routing.generate('api_owner_read');
function reloadTableOwner()
{
tbody.innerHTML = null;
AJAX('GET', ownersURL, function(request)
{
// Récupère le json et boucle pour créer le tableau
let json = JSON.parse(request.responseText);
for (let i = 0; i < json.length; i++)
{
......@@ -101,11 +104,17 @@
let td4 = document.createElement('td');
// Boutons edit
/*
* Editer une ligne du tableau (update),
* charge les données dans le formulaire
*/
let editBtn = document.createElement('button');
editBtn.dataset.ownerId = json[i].id;
editBtn.innerHTML = '<i class="fas fa-edit fa-fw"></i> Editer';
editBtn.classList.add('btn', 'btn-primary');
editBtn.addEventListener('click', function()
{
let ownerId = this.dataset.ownerId;
......@@ -122,11 +131,16 @@
});
td4. appendChild(editBtn);
// Boutons delete
/*
* Effacer une ligne du tableau (delete)
*/
let deleteBtn = document.createElement('button');
deleteBtn.dataset.ownerId = json[i].id;
deleteBtn.innerHTML = '<i class="fas fa-trash fa-fw"></i> Supprimer';
deleteBtn.classList.add('btn', 'btn-danger');
deleteBtn.addEventListener('click', function()
{
let ownerId = this.dataset.ownerId;
......@@ -142,23 +156,28 @@
});
td4. appendChild(deleteBtn);
tr.appendChild(td4);
tbody.appendChild(tr);
}
});
}
reloadTableOwner();
let form = document.querySelector('form[name="owner"]');
// form[name$="owner"] .. regex, qui finit par owner
// form[name^="owner"] .. regex, qui commence par owner
// Evénements sur les boutons
/*
* Affiche le formulaire (create)
*/
let createBtns = document.querySelectorAll('.btn-create');
createBtns.forEach(function(currentBtn){ // ou for (let currentBtn of createBtns)
currentBtn.addEventListener('click', function(e) { // 'e' veut dire 'event'
createBtns.forEach(function(currentBtn) // ou for (let currentBtn of createBtns)
{
currentBtn.addEventListener('click', function(e) // 'e' veut dire 'event'
{
form.querySelector('#owner_firstname').value = null;
form.querySelector('#owner_lastname').value = null;
form.parentNode.classList.remove('hidden');
......@@ -166,14 +185,25 @@
});
});
/*
* Recharge le tableau (read)
*/
let reloadBtns = document.querySelectorAll('.btn-reload');
reloadBtns.forEach(function(reloadBtn) {
reloadBtn.addEventListener('dblclick', function (e) {
reloadBtns.forEach(function(reloadBtn)
{
reloadBtn.addEventListener('dblclick', function (e)
{
reloadTableOwner();
});
});
// Soumission du formulaire
/*
* Soumission du formulaire (create ou update)
*/
form.addEventListener('submit', function(e)
{
let form = this;
......@@ -185,7 +215,6 @@
form.parentNode.classList.add('hidden');
},
form );
});
</script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment