Script CRUD Javascript!

 


Berikut adalah script HTMLnya : 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>CRUD dengan JavaScript</title>

<link rel="stylesheet" href="style.css">

</head>

<body>

<h1>CRUD dengan JavaScript</h1>


<form id="form">

<label for="name">Nama:</label>

<input type="text" id="name" name="name">


<label for="email">Email:</label>

<input type="email" id="email" name="email">


<label for="phone">Telepon:</label>

<input type="text" id="phone" name="phone">


<button type="submit" id="submit">Simpan</button>

</form>


<table>

<thead>

<tr>

<th>Nama</th>

<th>Email</th>

<th>Telepon</th>

<th>Aksi</th>

</tr>

</thead>

<tbody id="table-body">

</tbody>

</table>


<script src="script.js"></script>

</body>

</html>

Dan ini cssnya : 

body {

font-family: Arial, sans-serif;

}


h1 {

text-align: center;

}


form {

display: flex;

flex-direction: column;

align-items: center;

margin-bottom: 30px;

}


label {

margin-top: 10px;

}


input[type="text"],

input[type="email"] {

padding: 5px;

margin-left: 10px;

}


button {

padding: 5px 20px;

margin-top: 10px;

background-color: #008CBA;

color: white;

border: none;

border-radius: 5px;

cursor: pointer;

}


table {

margin: auto;

border-collapse: collapse;

}


table th,

table td {

padding: 10px;

border: 1px solid black;

}


table th {

background-color: #008CBA;

color: white;

}


table td:last-child {

text-align: center;

}


table td a {

color: red;

text-decoration: none;

cursor: pointer;

}

Yang terakhir merupakan script dari javascriptnya : 

// Data dummy
let data = [
{
id: 1,
name: 'John Doe',
email: 'johndoe@example.com',
phone: '08123456789'
},
{
id: 2,
name: 'Jane Doe',
email: 'janedoe@example.com',
phone: '08987654321'
}
];

// Fungsi untuk menampilkan data ke dalam tabel
function displayData() {
let tableBody = document.getElementById('table-body');
tableBody.innerHTML = '';

data.forEach(item => {
let tr = document.createElement('tr');
tr.innerHTML = `
<td>${item.name}</td>
<td>${item.email}</td>
<td>${item.phone}</td>
<td>
<a href="#" onclick="editData(${item.id})">Edit</a> |
<a href="#" onclick="deleteData(${item.id})">Hapus</a>
</td>
`;
tableBody.appendChild(tr);
});
}

// Fungsi untuk menambah data
function addData(name, email, phone) {
let id = data.length + 1;

let newData = {
id: id,
name: name,
email: email,
phone: phone
};

data.push(new

Comments