Make alias form a bit more HTML friendly

This commit is contained in:
Alexandre Iooss 2020-08-23 00:33:36 +02:00
parent 342d3910c7
commit 0ba77fb8f0
3 changed files with 55 additions and 49 deletions

View File

@ -1,37 +1,43 @@
/**
* On form submit, create a new alias
*/
function create_alias (e) {
// Do not submit HTML form
e.preventDefault();
$("#alias_input").on('keypress',function(e) { // Get data and send to API
if(e.which == 13) { const formData = new FormData(e.target);
$("#alias_submit").click(); $.post("/api/note/alias/", {
} "csrfmiddlewaretoken": formData.get("csrfmiddlewaretoken"),
}); "name": formData.get("name"),
"note": formData.get("note")
function create_alias(note_id){ }).done(function () {
$.post("/api/note/alias/", // Reload table
{ $("#alias_table").load(location.pathname + " #alias_table");
"csrfmiddlewaretoken": CSRF_TOKEN, addMsg("Alias ajouté", "success");
"name": $("#alias_input").val(), }).fail(function (xhr, _textStatus, _error) {
"note": note_id errMsg(xhr.responseJSON);
} });
).done(function(){
$("#alias_table").load(location.pathname+ " #alias_table");
addMsg("Alias ajouté","success");
})
.fail(function(xhr, textStatus, error){
errMsg(xhr.responseJSON);
});
} }
// on click of button "delete" , call the API
function delete_button(button_id){ /**
$.ajax({ * On click of "delete", delete the alias
url:"/api/note/alias/"+button_id+"/", * @param Integer button_id Alias id to remove
method:"DELETE", */
headers: {"X-CSRFTOKEN": CSRF_TOKEN} function delete_button (button_id) {
}) $.ajax({
.done(function(){ url: "/api/note/alias/" + button_id + "/",
addMsg('Alias supprimé','success'); method: "DELETE",
$("#alias_table").load(location.pathname + " #alias_table"); headers: { "X-CSRFTOKEN": CSRF_TOKEN }
}) }).done(function () {
.fail(function(xhr,textStatus, error){ addMsg('Alias supprimé', 'success');
errMsg(xhr.responseJSON); $("#alias_table").load(location.pathname + " #alias_table");
}); }).fail(function (xhr, _textStatus, _error) {
} errMsg(xhr.responseJSON);
});
}
$(document).ready(function () {
// Attach event
document.getElementById("form_alias").addEventListener("submit", create_alias);
})

View File

@ -6,15 +6,15 @@
<h3 class="card-header text-center"> <h3 class="card-header text-center">
{% trans "Note aliases" %} {% trans "Note aliases" %}
</h3> </h3>
<div class="card-body text-center"> <div class="card-body">
<div class="input-group"> <form class="input-group" method="POST" id="form_alias">
<input id="alias_input" type="text" class="form-control"> {% csrf_token %}
<input type="hidden" name="note" value="{{ object.note.pk }}">
<input type="text" name="name" class="form-control">
<div class="input-group-append"> <div class="input-group-append">
<button id="alias_submit" class="btn btn-success" onclick="create_alias({{ object.note.pk }})"> <input type="submit" class="btn btn-success" value="{% trans "Add" %}">
{% trans "Add" %}
</button>
</div> </div>
</div> </form>
</div> </div>
{% render_table aliases %} {% render_table aliases %}
</div> </div>

View File

@ -6,15 +6,15 @@
<h3 class="card-header text-center"> <h3 class="card-header text-center">
{% trans "Note aliases" %} {% trans "Note aliases" %}
</h3> </h3>
<div class="card-body text-center"> <div class="card-body">
<div class="input-group"> <form class="input-group" method="POST" id="form_alias">
<input id="alias_input" type="text" class="form-control"> {% csrf_token %}
<input type="hidden" name="note" value="{{ object.note.pk }}">
<input type="text" name="name" class="form-control">
<div class="input-group-append"> <div class="input-group-append">
<button id="alias_submit" class="btn btn-success" onclick="create_alias({{ object.note.pk }})"> <input type="submit" class="btn btn-success" value="{% trans "Add" %}">
{% trans "Add" %}
</button>
</div> </div>
</div> </form>
</div> </div>
{% render_table aliases %} {% render_table aliases %}
</div> </div>