如何更新HTML表中的Firebase数据库数据?

时间:2019-01-26 14:33:38

标签: javascript html firebase firebase-realtime-database

我已经在HTML表中显示了Firebase数据库数据

现在我要从HTML表中单击“批准”按钮以更新要批准的状态。 (单击第一行,仅更新第一条数据)

这是我要做的按钮的代码,但是我不能按我的要求工作。

function ButtonApproveClick(){
    var req = document.getElementById('Users');
    var dbRefReq = firebase.database().ref().child('Users')
    dbRefReq.once("child_added", function(snapshot ) {
        snapshot.ref.update({ statuss: "valid" })
    });
}

这是我的数据库,看起来像:[Firebase数据库]

谁能帮助我解决问题……

(function() {

var config = {


  apiKey: "AIzaSyDwrBm054kYrvm-6hZg3Ve_I0Rgp4MfOGo",
  authDomain: "petservice-f7559.firebaseapp.com",
  databaseURL: "https://petservice-f7559.firebaseio.com",
  projectId: "petservice-f7559",
  storageBucket: "petservice-f7559.appspot.com",
  messagingSenderId: "390073142865"
};
firebase.initializeApp(config);

  const req = document.getElementById('Users');

  const dbRefReq = firebase.database().ref().child('Users')

  dbRefReq.on('child_added', snap => {


      var name = snap.child("name").val();
      var phone = snap.child("phone").val();
      var statuss = snap.child("statuss").val();
      var icurl = snap.child("icurl").val();
      var userid = snap.child("userid").val();


$("#table_body").append("<tr><td>"+name+" </td><td>"+phone+"</td><td>"+ statuss+ "</td> <td><image src="+icurl+" alt=Trulli width=200 height=200> </image></td><td><button onclick='App()' dataid ="+userid+">Approve </button></td><td><button onclick='Dis()'>Remove</button></td></tr>");

  });

  }());



function App(){
 firebase.database().ref().child('Users').child(dataid).update({ statuss: "valid" })
}

function Dis(){

    alert("Hi2");

}
body {
  background: #fff;
  padding: 0px;
  margin: 0px;
  font-family: 'Nunito', sans-serif;
  font-size: 16px;
}

input, button {
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
}

.main-div, .loggedin-div {
  width: 20%;
  margin: 0px auto;
  margin-top: 150px;
  padding: 20px;
  display: none;
}

.main-div input {
  display: block;
  border: 1px solid #ccc;
  border-radius: 5px;
  background: #fff;
  padding: 15px;
  outline: none;
  width: 100%;
  margin-bottom: 20px;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}

.main-div input:focus {
  border: 1px solid #777;
}

.main-div button, .loggedin-div button {
  background: #5d8ffc;
  color: #fff;
  border: 1px solid #5d8ffc;
  border-radius: 5px;
  padding: 15px;
  display: block;
  width: 100%;
  transition: 0.3s;
  -webkit-transition: 0.3s;
  -moz-transition: 0.3s;
}

.main-div button:hover, .loggedin-div button:hover {
  background: #fff;
  color: #5d8ffc;
  border: 1px solid #5d8ffc;
  cursor: pointer;
}

<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
<html>


<head>
  <script>
  $(document).ready(function(){
    $("#myInput").on("keyup", function() {
      var value = $(this).val().toLowerCase();
      $("#A").filter(function() {
        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
      });
    });
  });
  </script>

  <title>Firebase Login</title>
  <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">

  <link rel="stylesheet" href="style.css" />
</head>
<body>


<h1>HI</h1>
<input id="myInput" type="text" placeholder="Search..">

<div class="mainDiv" align="left">
  <h1>ALL User</h>
    <table >
      <thead>
        <tr>
          <td>Name</td>
          <td>Phone</td>
          <td>Status</td>
          <td>Url</td>
            <td>Approve</td>
              <td>Disapprove</td>
        </tr>
      </thead>

      <tbody id="table_body">


      </tbody>

    </table>
  </div>



  <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script>

          <script src="https://code.jquery.com/jquery-3.1.0.js"></script>


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

</body>
</html>

1 个答案:

答案 0 :(得分:1)

这是一个三步过程。由于您没有共享完整的代码/ HTML,因此,我仅向您提供所需的步骤:

  1. 确保ButtonApproveClick知道用户单击了哪个特定按钮。
  2. 然后在该行中找到子节点的Firebase密钥/ ID(通常通过将密钥/ ID存储在该行的id属性中)。
  3. 最后将密钥/标识传递给数据库调用以进行更新:firebase.database().ref().child('Users').child(keyOfUserThatWasClicked).update({ statuss: "valid" })。您在这里不需要once()侦听器。

有关如何呈现用户的简单示例:

firebase.database().ref().child('Users').on('value', function(snapshot) {
  snapshot.forEach(function(userSnapshot) {
    var tr = document.createElement('tr');
    tr.id = userSnapshot.key;
    // TODO: populate the rest of the row and add it to the table
});

然后,您可能会获得用户单击的行的ID:

function ButtonApproveClick(e){
    var button = e.target;
    var tr = e.parentElement.parentElement; // might need more or fewer to reach the right/TR element in the HTML
    var key = tr.id;
    firebase.database().ref().child('Users').child(key).update({ statuss: "valid" })
}