v0.2.0 - Added CRUD for user
This commit is contained in:
@@ -16,7 +16,7 @@ function createPost(title, desc) {
|
||||
title,
|
||||
'<a class="w-1em" onclick="editPost('+ data[0] + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
|
||||
'<a class="w-1em" onClick="deletePostSwal(' + data[0] + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
|
||||
]).draw().node().id = '1234';
|
||||
]).draw().node().id = data[0];
|
||||
},
|
||||
error: function (data) {
|
||||
console.log("error");
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
function createUser(name, pass) {
|
||||
var data = {
|
||||
name: name,
|
||||
password: pass,
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: '../Backend/controller/createUser.php',
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: data,
|
||||
success: function (data) {
|
||||
$('#table_id_users').DataTable().row.add([
|
||||
name,
|
||||
'<a class="w-1em" onclick="editUser('+ data + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
|
||||
'<a class="w-1em" onClick="deleteUserSwal(' + data + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
|
||||
]).draw().node().id = data;
|
||||
},
|
||||
error: function (data) {
|
||||
console.log("error");
|
||||
console.log(data);
|
||||
console.log("error");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
function deleteUser(id) {
|
||||
return $.ajax({
|
||||
type: 'POST',
|
||||
url: '../Backend/controller/deleteUser.php',
|
||||
data: {'delete_id':id},
|
||||
success:function (data) {
|
||||
Swal.fire(
|
||||
'Brugeren er slettet!',
|
||||
'',
|
||||
'success'
|
||||
).then(function() {
|
||||
$('#table_id_users').DataTable().row($('#'+id)[0]).remove().draw();
|
||||
});
|
||||
},
|
||||
error:function (data) {
|
||||
Swal.fire(
|
||||
'Brugeren kunne ikke blive slettet',
|
||||
'',
|
||||
'error'
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
function editUser(id) {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '../Backend/controller/getUser.php',
|
||||
data: { id: id },
|
||||
success:function (data) {
|
||||
Swal.fire({
|
||||
title: 'Rediger Bruger',
|
||||
html: '<label class="col-md-12 col-form-label text-md-right">Lad password forbliv blank, hvis det ikke skal ændres</label><div class="form-group row mb-4 justify-content-center"><label for="name" class="col-md-3 col-form-label text-md-right">Navn</label><div class="col-md-9"><input type="text" class="form-control" id="name" name="name" value="' + data[0].name + '" maxlength="255" required autofocus></div></div><div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-3 col-form-label text-md-right">Password</label><div class="col-md-9"><input type="password" class="form-control" id="password" name="password" maxlength="255" required autofocus></div></div><div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-3 col-form-label text-md-right">Confirm Password</label><div class="col-md-9"><input type="password" class="form-control" id="confirmPassword" name="confirmPassword" maxlength="255" required autofocus></div></div>',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Rediger',
|
||||
focusConfirm: false,
|
||||
preConfirm: () => {
|
||||
const name = Swal.getPopup().querySelector('#name').value
|
||||
const pass = Swal.getPopup().querySelector('#password').value
|
||||
const conpass = Swal.getPopup().querySelector('#confirmPassword').value
|
||||
if (!name) {
|
||||
Swal.showValidationMessage(`Brugeren skal have et navn!`)
|
||||
} else if (pass != conpass) {
|
||||
Swal.showValidationMessage(`Password er ikke ens!`)
|
||||
} else {
|
||||
data.name = name;
|
||||
data.password = pass;
|
||||
}
|
||||
return {name: name, pass: pass, conpass: conpass}
|
||||
}
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed)
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '../Backend/controller/updateUser.php',
|
||||
data: {'id':id, 'upName': data.name, 'upPassword': data.password},
|
||||
success:function () {
|
||||
Swal.fire(
|
||||
'Brugeren er redigeret!',
|
||||
'',
|
||||
'success'
|
||||
).then(function() {
|
||||
document.getElementById(id).children[0].innerHTML = data.name;
|
||||
})
|
||||
},
|
||||
error:function (data) {
|
||||
Swal.fire(
|
||||
'Brugeren kunne ikke blive redigeret',
|
||||
'',
|
||||
'error'
|
||||
)
|
||||
}
|
||||
});
|
||||
})
|
||||
},
|
||||
error:function (data) {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
/**/
|
||||
}
|
||||
@@ -12,11 +12,21 @@ $(document).ready(function() {
|
||||
],
|
||||
});
|
||||
|
||||
var tableUser = $('#table_id_users').DataTable({
|
||||
fixedHeader: true,
|
||||
columnDefs: [
|
||||
{
|
||||
"targets": "w-1em",
|
||||
orderable: false,
|
||||
"searchable": false,
|
||||
}
|
||||
],
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '../Backend/controller/getPost.php',
|
||||
success:function (data) {
|
||||
console.log(data);
|
||||
for (let i = 0; i < data.length; i++){
|
||||
var d = new Date(data[i].created_at);
|
||||
|
||||
@@ -32,4 +42,29 @@ $(document).ready(function() {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '../Backend/controller/getUser.php',
|
||||
success:function (data) {
|
||||
for (let i = 0; i < data[0].length; i++){
|
||||
if (data[0][i].id != data[1]) {
|
||||
tableUser.row.add([
|
||||
data[0][i].name,
|
||||
'<a class="w-1em" onclick="editUser(' + data[0][i].id + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
|
||||
'<a class="w-1em" onClick="deleteUserSwal(' + data[0][i].id + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
|
||||
]).draw().node().id = data[0][i].id;
|
||||
} else {
|
||||
tableUser.row.add([
|
||||
data[0][i].name,
|
||||
'<a class="w-1em" onclick="editUser(' + data[0][i].id + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
|
||||
''
|
||||
]).draw().node().id = data[0][i].id;
|
||||
}
|
||||
}
|
||||
},
|
||||
error:function (data) {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user