61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
var table = $('#table-list').DataTable({
|
|
"ajax": {
|
|
"url": "http://localhost:11000/api/read",
|
|
"type": "POST",
|
|
"dataSrc": "data",
|
|
"contentType": "application/json",
|
|
"data": function(d) {
|
|
// Customize the data payload sent in the POST request
|
|
return JSON.stringify({});
|
|
},
|
|
"error": function (xhr, error, thrown) {
|
|
console.error('Error fetching data:', thrown);
|
|
console.error('Response:', xhr.responseText);
|
|
}
|
|
},
|
|
"columns": [
|
|
{
|
|
"data": "id",
|
|
"render": function(data, type, row) {
|
|
return data;
|
|
}
|
|
},
|
|
{
|
|
"data": "name",
|
|
"render": function(data, type, row) {
|
|
return data;
|
|
}
|
|
},
|
|
{
|
|
"data": "phone",
|
|
"render": function(data, type, row) {
|
|
return data;
|
|
}
|
|
},
|
|
{
|
|
"data": null,
|
|
"render": function(data, type, row) {
|
|
return `<button>Update</button> <button>Delete</button>`;
|
|
}
|
|
},
|
|
],
|
|
})
|
|
|
|
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");
|
|
}
|
|
|