【问题标题】:How to delete the previous data如何删除以前的数据
【发布时间】:2018-10-18 00:25:40
【问题描述】:

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

我想删除以前的数据,因为每当新数据存储在 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>

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database


    【解决方案1】:

    最简单的方法是在表格中添加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_ 事件,它会为您提供更精确地更新表的信息,而不是上面的暴力刷新。

    【讨论】:

    • 您好弗兰克先生 美好的一天!谢谢回答我的问题。我是 firebase 和 javascript 语言的新手,但您的回答立即解决了我的问题,再次感谢上帝保佑 ^^
    • 顺便说一句,先生,我已经获得了我的图片的网址。有什么办法可以在桌子上展示吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多