gpt4 book ai didi

Javascript 显示本地存储阵列中的数据问题

转载 作者:行者123 更新时间:2023-12-02 18:34:24 24 4
gpt4 key购买 nike

我正在使用 Javascript 创建一个简单的 CRUD 应用程序,但在显示本地存储中的数据表时遇到问题。

这就像制作一个 TODO 列表,但这是一个表格,并在一行和一列中输入数据。

我不知道如何清楚地解释这个问题,重要的是我希望输入表单并提交到本地存储的结果能够正确显示在表格中。

这是代码:

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>

这是表中的问题,数据与列不匹配。

enter image description here

而我想要的实际结果是这样的。

enter image description here

最佳答案

做类似的事情...

const 
myForm = document.forms['my-form'] // unique reference for ALL form's elements
, myTableTBody = document.querySelector('#myTable tbody')
, dataNL = JSON.parse(localStorage.getItem('dataNL') || '[]')
;

// init table
dataNL.forEach(row=>
{
let newRow = myTableTBody.insertRow()

newRow.insertCell().textContent = row.no
newRow.insertCell().textContent = row.inputNL
newRow.insertCell().textContent = row.inputJK
newRow.insertCell().textContent = row.inputNH
newRow.insertCell().textContent = row.inputA
newRow.insertCell().textContent = ''
})

myForm.onsubmit = e =>
{
e.preventDefault()

// add entries into local storage :
dataNL.push(Object.fromEntries(new FormData(myForm).entries()))
localStorage.setItem('dataNL', JSON.stringify( dataNL ))

// insert new row

let newRow = myTableTBody.insertRow()

newRow.insertCell().textContent = myForm.no.value
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().textContent = '' // Action
}

-> some help <-

关于Javascript 显示本地存储阵列中的数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68987657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com