some ajax
This commit is contained in:
parent
3a1885d551
commit
9c0da4c17e
1 changed files with 35 additions and 0 deletions
35
hackens_orga/frontend/static/js/budget_list.js
Normal file
35
hackens_orga/frontend/static/js/budget_list.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
|
||||||
|
const $deletebudgetline = Array.prototype.slice.call(document.querySelectorAll('.delete-budgetline'), 0);
|
||||||
|
|
||||||
|
// Add a click event on each of them
|
||||||
|
$deletebudgetline.forEach(el => {
|
||||||
|
el.addEventListener('click', async () => {
|
||||||
|
// Get the target from the "data-target" attribute
|
||||||
|
if ('lineid' in el.dataset) {
|
||||||
|
const target = el.dataset.lineid;
|
||||||
|
const tableLine = document.getElementById(`tableline-${target}`);
|
||||||
|
if (confirm("Are you sure to delete this item ?")) {
|
||||||
|
// Get csrf token
|
||||||
|
const cookieValue = document.cookie
|
||||||
|
.split('; ')
|
||||||
|
.find((row) => row.startsWith('csrftoken='))
|
||||||
|
?.split('=')[1];
|
||||||
|
const url = `/api/budget/budgetline/${target}/`;
|
||||||
|
await fetch(url, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
'X-CSRFToken': cookieValue
|
||||||
|
}
|
||||||
|
}).then((resp) => {
|
||||||
|
if(resp.ok) {
|
||||||
|
tableLine.remove();
|
||||||
|
//TODO: Add a message (code a helper for that) for succes or error
|
||||||
|
}
|
||||||
|
}).catch((e) => console.log(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in a new issue