Fix CRUD on karts

This commit is contained in:
bglacial 2019-04-18 23:02:52 +02:00
parent e04074843a
commit ee2bd88f93
2 changed files with 55 additions and 22 deletions

View File

@ -35,15 +35,15 @@ function getPilotsByKart() {
"<div class=\"card shadow mb-4\">\n" + "<div class=\"card shadow mb-4\">\n" +
" <div class=\"card-header py-3\">\n" + " <div class=\"card-header py-3\">\n" +
" <h6 class=\"m-0 font-weight-bold text-primary\">\n" + " <h6 class=\"m-0 font-weight-bold text-primary\">\n" +
" Pilot list - "+value.name+" \n" + " Pilot list - " + value.name + " \n" +
" <a href=\"#\" class=\"btn btn-primary btn-circle\" title='Ajouter un piote au karting "+value.name+"'>\n" + " <a href=\"#\" class=\"btn btn-primary btn-circle\" title='Ajouter un piote au karting " + value.name + "'>\n" +
" <i class=\"fas fa-plus\"></i>\n" + " <i class=\"fas fa-plus\"></i>\n" +
" </a>\n" + " </a>\n" +
" </h6>\n" + " </h6>\n" +
" </div>\n" + " </div>\n" +
" <div class=\"card-body\">\n" + " <div class=\"card-body\">\n" +
" <div class=\"table-responsive\">\n" + " <div class=\"table-responsive\">\n" +
" <table class=\"table table-bordered\" id=\"dataTable_pilots"+value.id+"\" width=\"100%\" cellspacing=\"0\">\n" + " <table class=\"table table-bordered\" id=\"dataTable_pilots" + value.id + "\" width=\"100%\" cellspacing=\"0\">\n" +
" <thead>\n" + " <thead>\n" +
" <tr>\n" + " <tr>\n" +
" <th></th>\n" + " <th></th>\n" +
@ -62,7 +62,7 @@ function getPilotsByKart() {
" </div>" + " </div>" +
""); "");
$('#dataTable_pilots'+value.id).DataTable({ $('#dataTable_pilots' + value.id).DataTable({
data: value.drivers, data: value.drivers,
columns: [ columns: [
{ {
@ -142,7 +142,7 @@ function getKarts(id) {
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
crossDomain: true, crossDomain: true,
url: api_url + 'karts/'+id+'/', url: api_url + 'karts/' + id + '/',
dataType: 'json', dataType: 'json',
success: function (data, status) { success: function (data, status) {
$('#id').val(data.id); $('#id').val(data.id);
@ -165,7 +165,7 @@ function listKarts() {
url: api_url + 'karts/', url: api_url + 'karts/',
dataType: 'json', dataType: 'json',
success: function (data, status) { success: function (data, status) {
if(tableListKart instanceof $.fn.dataTable.Api){ if (tableListKart instanceof $.fn.dataTable.Api) {
tableListKart.destroy(); tableListKart.destroy();
} }
tableListKart = $('#dataTable_kart_list').DataTable({ tableListKart = $('#dataTable_kart_list').DataTable({
@ -174,13 +174,13 @@ function listKarts() {
{ {
data: "id", data: "id",
render: function (data, type, row, meta) { render: function (data, type, row, meta) {
return '<a href="#" onclick="" class="btn btn-primary btn-circle">' + return '<a href="#" onclick="modalEditKarts(' + data + ')" class="btn btn-primary btn-circle">' +
'<i class="fas fa-edit"></i>' + '<i class="fas fa-edit"></i>' +
'</a>' + '</a>' +
'&nbsp;' + '&nbsp;' +
'<a href="#" onclick="if ( confirm( \'Do you want to delete the kart\' ) ) {deleteKarts('+data+','+meta.row+')}" class="btn btn-danger btn-circle">' + '<a href="#" onclick="if ( confirm( \'Do you want to delete the kart\' ) ) {deleteKarts(' + data + ',' + meta.row + ')}" class="btn btn-danger btn-circle">' +
'<i class="fas fa-trash"></i>' + '<i class="fas fa-trash"></i>' +
'</a>'; '</a>';
} }
}, },
{ {
@ -203,7 +203,7 @@ function deleteKarts(id, index) {
$.ajax({ $.ajax({
type: 'DELETE', type: 'DELETE',
crossDomain: true, crossDomain: true,
url: api_url + 'karts/'+id+'/', url: api_url + 'karts/' + id + '/',
dataType: 'json', dataType: 'json',
success: function (data, status) { success: function (data, status) {
$.notify("Delete done!", "success"); $.notify("Delete done!", "success");
@ -227,6 +227,7 @@ function addKarts(data) {
$.notify("Update done!", "success"); $.notify("Update done!", "success");
$('#modal_newkart').modal('hide'); $('#modal_newkart').modal('hide');
$("#saveKartChanges").prop('disabled', false); $("#saveKartChanges").prop('disabled', false);
$("#saveKartChanges").closest('form').find("input, textarea").val("");
listKarts() listKarts()
}, },
error: function (xhr) { error: function (xhr) {
@ -237,16 +238,18 @@ function addKarts(data) {
}); });
} }
function editKarts(data) { function modalEditKarts(id) {
$('#modal_newkart').modal('show');
$.ajax({ $.ajax({
type: 'PUT', type: 'GET',
crossDomain: true, crossDomain: true,
url: api_url + 'karts/1/', url: api_url + 'karts/'+id+'/',
dataType: 'json', dataType: 'json',
data: data,
success: function (data, status) { success: function (data, status) {
$.notify("Update done!", "success"); $('#id').val(data.id);
showKarts(); $('#name').val(data.name);
$('#autonomy').val(data.autonomy);
$('#comment').val(data.comment);
}, },
error: function (xhr) { error: function (xhr) {
$.notify("APIs unreachable!", "error"); $.notify("APIs unreachable!", "error");
@ -255,12 +258,34 @@ function editKarts(data) {
}); });
} }
function editKarts(id, data) {
$.ajax({
type: 'PUT',
crossDomain: true,
url: api_url + 'karts/'+id+'/',
dataType: 'json',
data: data,
success: function (data, status) {
$.notify("Update done!", "success");
$('#modal_newkart').modal('hide');
$("#saveKartChanges").prop('disabled', false);
$("#saveKartChanges").closest('form').find("input, textarea").val("");
listKarts()
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
$("#saveKartChanges").prop('disabled', false);
},
});
}
function getDriver(id) { function getDriver(id) {
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
crossDomain: true, crossDomain: true,
url: api_url + 'drivers/'+id+'/', url: api_url + 'drivers/' + id + '/',
dataType: 'json', dataType: 'json',
success: function (data, status) { success: function (data, status) {
$('#id').val(data.id); $('#id').val(data.id);

View File

@ -307,9 +307,17 @@
$("#kartForm").submit(function (event) { $("#kartForm").submit(function (event) {
$("#saveKartChanges").prop('disabled', true); $("#saveKartChanges").prop('disabled', true);
event.preventDefault(); event.preventDefault();
addKarts($( this ).serialize()) if($('#id').val() == ""){
addKarts($( this ).serialize())
}else{
editKarts($('#id').val(), $( this ).serialize())
}
}); });
$('#modal_newkart').on('hide.bs.modal', function () {
$("#saveKartChanges").closest('form').find("input, textarea").val("");
})
}); });
</script> </script>