Commit f29eac94 authored by Mat's avatar Mat

boutons delete: l'action, la route, la fonction ajax

parent 675b30f2
...@@ -14,14 +14,6 @@ api_owner_read_one: ...@@ -14,14 +14,6 @@ api_owner_read_one:
_controller: APIBundle:Owner:readOne _controller: APIBundle:Owner:readOne
methods: [ GET ] methods: [ GET ]
api_image_update:
path: /image/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:Image:update
methods: [ POST ]
api_owner_create: api_owner_create:
path: /owner/add path: /owner/add
defaults: defaults:
...@@ -36,6 +28,23 @@ api_owner_update: ...@@ -36,6 +28,23 @@ api_owner_update:
id: \d+ id: \d+
methods: [ POST ] methods: [ POST ]
api_owner_delete_one:
path: /owner/{id}
defaults:
_controller: APIBundle:Owner:deleteOne
requirements:
id: \d+
methods: [ DELETE ]
api_image_update:
path: /image/{id}
requirements:
id: \d+
defaults:
_controller: APIBundle:Image:update
methods: [ POST ]
## ##
## Note: ## Note:
## ##
...@@ -43,5 +52,5 @@ api_owner_update: ...@@ -43,5 +52,5 @@ api_owner_update:
## Pour l'interroger de l'extérieur, depuis un autre domaine, il faut installer un Bundle : ## Pour l'interroger de l'extérieur, depuis un autre domaine, il faut installer un Bundle :
## NelmioBundle, avec composer.phar require ... ## NelmioBundle, avec composer.phar require ...
## Sinon il se produit un blocage cross-origin. ## Sinon il se produit un blocage cross-origin.
## Recherche: cross origin symfony ## Recherche: cross origin symfony
## ##
...@@ -96,19 +96,16 @@ ...@@ -96,19 +96,16 @@
tr.appendChild(td3); tr.appendChild(td3);
let td4 = document.createElement('td'); let td4 = document.createElement('td');
let editBtn = document.createElement('button');
// W3C cette notation est conseillée pour un attr custom : // Boutons edit
let editBtn = document.createElement('button');
editBtn.dataset.ownerId = json[i].id; editBtn.dataset.ownerId = json[i].id;
editBtn.innerHTML = '<i class="fas fa-edit fa-fw"></i> Editer'; editBtn.innerHTML = '<i class="fas fa-edit fa-fw"></i> Editer';
editBtn.classList.add('btn', 'btn-primary'); editBtn.classList.add('btn', 'btn-primary');
// Evénement sur les boutons
editBtn.addEventListener('click', function() editBtn.addEventListener('click', function()
{ {
let ownerId = this.dataset.ownerId; let ownerId = this.dataset.ownerId;
submitURL = Routing.generate('api_owner_update', {'id': ownerId}); submitURL = Routing.generate('api_owner_update', {'id': ownerId});
getAJAX(submitURL, function() getAJAX(submitURL, function()
{ {
if (this.readyState === 4 && this.status === 200) if (this.readyState === 4 && this.status === 200)
...@@ -119,15 +116,37 @@ ...@@ -119,15 +116,37 @@
form.parentNode.classList.remove('hidden'); form.parentNode.classList.remove('hidden');
} }
}); });
}); });
td4. appendChild(editBtn); td4. appendChild(editBtn);
tr.appendChild(td4);
// '<a class="btn btn-danger" href="#"> // Boutons delete
// <i class="fas fa-trash fa-fw"></i> Supprimer let deleteBtn = document.createElement('button');
// </a>'; 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;
let r = confirm('Are you sure ?');
if (r === true) {
submitURL = Routing.generate('api_owner_delete_one', {'id': ownerId});
deleteAJAX(submitURL, function()
{
if (this.readyState === 4) {
if (this.status === 200)
{
reloadTableOwner();
}
else if (this.status === 400) {
console.log('BAD REQUEST. DISPLAY ERRORS');
}
}
});
}
});
td4. appendChild(deleteBtn);
tr.appendChild(td4);
tbody.appendChild(tr); tbody.appendChild(tr);
} }
......
...@@ -24,6 +24,14 @@ function postAJAX(url, callback, post) ...@@ -24,6 +24,14 @@ function postAJAX(url, callback, post)
xhr.send(new FormData(post)); xhr.send(new FormData(post));
} }
function deleteAJAX(url, callback)
{
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = callback;
xhr.open("DELETE", url, true);
xhr.send(null);
}
/* // pour info /* // pour info
function getAJAX_v2(url, callback) function getAJAX_v2(url, callback)
{ {
......
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