gpt4 book ai didi

javascript - 如何删除之前的数据

转载 作者:行者123 更新时间:2023-12-03 00:55:34 28 4
gpt4 key购买 nike

如何显示当前数据而不是获取多个数据。

我想删除以前的数据,因为每当新数据存储在 Firebase 上时,它都会复制相同的数据大约 4 次。

我正在使用移动应用将数据发送到 Firebase。我已将 databaseref.once 更改为 databaseref.on,因此它可以是实时数据库,但多个数据存储在我的表中。

<html>
<head>
<title>Firebase Realtime Database Web</title>
<script>
// firebase config here
</script>
</head>
<body>
<h3>Police Station 1</h3>
<table id="reports" border="1">
<tr>
<th>Email Address</th>
<th>Caption</th>
<th>Location</th>
<th>Time</th>
<th>Picture</th>
</tr>
</table>

<script>
var tblUsers = document.getElementById('reports');
var databaseRef = firebase.database().ref('PP3/');
var rowIndex = 1;

databaseRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();

var row = tblUsers.insertRow(rowIndex);
var email = row.insertCell(0);
var caption = row.insertCell(1);
var location = row.insertCell(2);
var time = row.insertCell(3);
var picture = row.insertCell(4);
email.appendChild(document.createTextNode(childData.Email.replace(/\\/g, '').replace(/"/g, '')));
caption.appendChild(document.createTextNode(childData.Caption.replace(/\\/g, '').replace(/"/g, '')));
location.appendChild(document.createTextNode(childData.Location.replace(/\\/g, '').replace(/"/g, '')));
time.appendChild(document.createTextNode(childData.Time.replace(/\\/g, '').replace(/"/g, '')));
picture.appendChild(document.createTextNode(childData.Picture.replace(/\\/g, '').replace(/"/g, '')));
rowIndex = rowIndex + 1;
});
});


</script>
</body>
</html>

最佳答案

最简单的方法是将 tbody 元素添加到表格中:

 <table id="reports" border="1">
<tr>
<th>Email Address</th>
<th>Caption</th>
<th>Location</th>
<th>Time</th>
<th>Picture</th>
</tr>
<tbody id="reportbody"></tbody>
</table>

然后在添加数据之前清除它:

<script>
var tblUsers = document.getElementById('reportbody');
var databaseRef = firebase.database().ref('PP3/');
var rowIndex = 1;

databaseRef.on('value', function(snapshot) {
tblUsers.innerHTML = '';
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();

var row = tblUsers.insertRow(rowIndex);
...

请注意,虽然简单,但效率不是很高,因此您可能会看到一些闪烁。如果这是一个问题,请考虑在 Firebase 中监听 child_ 事件,该事件会为您提供信息以更精确地更新表,而不是上面的强力刷新。

关于javascript - 如何删除之前的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52857835/

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