I'm creating a Simple CRUD Application with Javascript and I'm having a problem in Display Data Table from local Storage.
It's like making a TODO List but this is with a Table and enter the data in one row and in a column.
I don't know how to explain the problem clearly, the important thing is that I want the results from my input form and submit to local storage to be displayed correctly in the table.
This is The Code :
const
myForm = document.forms['my-form'] // form name pada tag html form
, table = document.querySelector('#myTable tbody')
, dataArr = JSON.parse(localStorage.getItem('dataArr') || '[]');
var selectedRow = null
var number = 0
// Tabel init
dataArr.forEach(row=>{
let newRow = table.insertRow()
newRow.insertCell().textContent = row.no = ++number
newRow.insertCell().textContent = row.inputNL
newRow.insertCell().textContent = row.inputJK
newRow.insertCell().textContent = row.inputNH
newRow.insertCell().textContent = row.inputA
newRow.insertCell().innerHTML = `<a onClick="onDelete(this)"><i class="fas fa-times"></i></a>`
})
function onFormSubmit() {
if (validate()) {
if (selectedRow == null) {
// Menambahkan entri ke LocalStorage :
dataArr.push(Object.fromEntries(new FormData(myForm).entries()))
localStorage.setItem('dataArr', JSON.stringify( dataArr ))
// Memasukkan Data pada Baris Baru
let newRow = table.insertRow()
newRow.insertCell().textContent = myForm.no.value = ++number
newRow.insertCell().textContent = myForm.inputNL.value
newRow.insertCell().textContent = myForm.inputJK.value
newRow.insertCell().textContent = myForm.inputNH.value
newRow.insertCell().textContent = myForm.inputA.value
newRow.insertCell().innerHTML = `<a onClick="onDelete(this)"><i class="fas fa-times"></i></a>`
}
resetForm();
}
}
// Function untuk Mereset Data yang sudah diSumbit di dalam Input
function resetForm() {
document.getElementById("no").value = "";
document.getElementById("inputNL").value = "";
document.getElementById("inputJK").value = "Pria";
document.getElementById("inputNH").value = "";
document.getElementById("inputA").value = "";
selectedRow = null;
}
// Function untuk Delete Data
function onDelete(td) {
if (confirm('Apa kamu yakin ingin Menghapus Data ini?')) {
row = td.parentElement.parentElement;
document.getElementById("myTable").deleteRow(row.rowIndex);
resetForm();
}
hapusDataLocal(td.parentElement.parentElement)
}
function hapusDataLocal(dataItem) {
dataArr.forEach(function(task, index) {
if (dataItem.textContent === task ) {
dataArr.splice(index, 1);
}
});
localStorage.setItem(dataArr, JSON.stringify(dataArr))
}
// Function untuk Memvalidasi Data
function validate() {
isValid = true;
if (document.getElementById("inputNL").value == "" || document.getElementById("inputJK").value == "" || document.getElementById("inputNH").value == "" || document.getElementById("inputA").value == "") {
isValid = false;
alert("Isi Semua Formnya dengan Benar!");
}
return isValid;
}
<!-- Registration Form -->
<div class="container w-50 mt-3 shadow p-3 mb-5 bg-white rounded">
<div class="p-3">
<h1 class="mb-4">Registration Form</h1>
<form class="task-form" name="my-form" onsubmit="event.preventDefault();onFormSubmit();" autocomplete="off">
<input type="hidden" name="no" id="no" value="">
<label for="inputNL" class="form-label">Nama Lengkap</label>
<input type="text" id="inputNL" name="inputNL" class="form-control mb-2" placeholder="Nama Lengkap">
<label for="inputJK"class="form-label">Jenis Kelamin</label>
<select class="form-select mb-2" id="inputJK" name="inputJK">
<option value="Pria">Pria</option>
<option value="Wanita">Wanita</option>
</select>
<label for="inputNH" class="form-label">No HP</label>
<input type="number" id="inputNH" name="inputNH" class="form-control mb-2" placeholder="No HP">
<label for="inputA" class="form-label">Alamat</label>
<textarea id="inputA" name="inputA" class="form-control mb-2" style="min-width: 100%" placeholder="Alamat"></textarea>
<input type="submit" value="Submit" class="btn btn-primary btn-md my-3">
</form>
</div>
</div>
<!-- Table -->
<div class="container mt-3 shadow p-3 mb-5 bg-white rounded">
<div class="p-3">
<table class="table" id="myTable">
<thead>
<th scope="col">#</th>
<th scope="col">Nama Lengkap</th>
<th scope="col">Jenis Kelamin</th>
<th scope="col">No HP</th>
<th scope="col">Alamat</th>
<th scope="col">Aksi</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
Here is The Problem in The Table, The data does not match the column.
And the actual result I want is like this.