Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 52afee5e25 | |||
| 7c00802a77 |
@ -4,9 +4,9 @@
|
|||||||
<link rel="stylesheet" href="//cdn.datatables.net/2.3.2/css/dataTables.dataTables.min.css">
|
<link rel="stylesheet" href="//cdn.datatables.net/2.3.2/css/dataTables.dataTables.min.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<input type="text" placeholder="Name" />
|
<input id="form-add-name" type="text" placeholder="Name" />
|
||||||
<input type="text" placeholder="Phone" />
|
<input id="form-add-phone" type="text" placeholder="Phone" />
|
||||||
<button>Create</button>
|
<button id="button-add" type="button" onclick="buttonAdd()">Create</button>
|
||||||
<hr>
|
<hr>
|
||||||
<table id="table-list">
|
<table id="table-list">
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
61
js/data.js
61
js/data.js
@ -1,3 +1,7 @@
|
|||||||
|
function buttonAct(id) {
|
||||||
|
return `<button onclick="buttonEdit(${id})">Update</button> <button onclick="buttonRemove(${id})">Delete</button>`
|
||||||
|
}
|
||||||
|
|
||||||
var table = $('#table-list').DataTable({
|
var table = $('#table-list').DataTable({
|
||||||
"ajax": {
|
"ajax": {
|
||||||
"url": "http://localhost:11000/api/read",
|
"url": "http://localhost:11000/api/read",
|
||||||
@ -23,20 +27,71 @@ var table = $('#table-list').DataTable({
|
|||||||
{
|
{
|
||||||
"data": "name",
|
"data": "name",
|
||||||
"render": function(data, type, row) {
|
"render": function(data, type, row) {
|
||||||
return data;
|
return `<input id="form-edit-name-${row.id}" type="text" value="${data}">`;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"data": "phone",
|
"data": "phone",
|
||||||
"render": function(data, type, row) {
|
"render": function(data, type, row) {
|
||||||
return data;
|
return `<input id="form-edit-phone-${row.id}" type="text" value="${data}">`;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"data": null,
|
"data": null,
|
||||||
"render": function(data, type, row) {
|
"render": function(data, type, row) {
|
||||||
return `<button>Update</button> <button>Delete</button>`;
|
return buttonAct(row.id);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function buttonAdd() {
|
||||||
|
var name = document.getElementById("form-add-name" ).value;
|
||||||
|
var phone = document.getElementById("form-add-phone" ).value;
|
||||||
|
var url = "http://localhost:11000/api/create";
|
||||||
|
var payload = {
|
||||||
|
"name" : name,
|
||||||
|
"phone" : phone
|
||||||
|
};
|
||||||
|
sendHttpRequest(url, "POST", payload, function (error, response) {
|
||||||
|
if (error) console.error("Error:", error);
|
||||||
|
else {
|
||||||
|
table.ajax.reload(null, false); // false means keep the current page
|
||||||
|
console.log("JSON Response:", response);
|
||||||
|
}
|
||||||
|
}, "application/json");
|
||||||
|
}
|
||||||
|
|
||||||
|
function buttonEdit(key){
|
||||||
|
var name = document.getElementById(`form-edit-name-${key}` ).value;
|
||||||
|
var phone = document.getElementById(`form-edit-phone-${key}` ).value;
|
||||||
|
var url = "http://localhost:11000/api/update";
|
||||||
|
var payload = {
|
||||||
|
"key" : key,
|
||||||
|
"name" : name,
|
||||||
|
"phone" : phone
|
||||||
|
};
|
||||||
|
sendHttpRequest(url, "POST", payload, function (error, response) {
|
||||||
|
if (error) console.error("Error:", error);
|
||||||
|
else {
|
||||||
|
table.ajax.reload(null, false); // false means keep the current page
|
||||||
|
console.log("JSON Response:", response);
|
||||||
|
}
|
||||||
|
}, "application/json");
|
||||||
|
alert("Data telah di Update");
|
||||||
|
}
|
||||||
|
|
||||||
|
function buttonRemove(key){
|
||||||
|
var url = "http://localhost:11000/api/delete";
|
||||||
|
var payload = {
|
||||||
|
"key" : key
|
||||||
|
};
|
||||||
|
sendHttpRequest(url, "POST", payload, function (error, response) {
|
||||||
|
if (error) console.error("Error:", error);
|
||||||
|
else {
|
||||||
|
table.ajax.reload(null, false); // false means keep the current page
|
||||||
|
console.log("JSON Response:", response);
|
||||||
|
}
|
||||||
|
}, "application/json");
|
||||||
|
alert("Data telah di Hapus Selamanya");
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user