繁体   English   中英

单击按钮时,firebase 删除数据库上的 ID

[英]firebase remove ID on database when click button

我正在做订单店项目,并且当人们订购并添加到数据库时,订单列表页面与 firebase 中的实时数据库一起工作,订单将更新到这样的订单列表表: https ://i.imgur.com/NjtYHSo.png 和在删除按钮我想在烹饪完成的食物时,单击此按钮,订购食物将从数据库中删除。 但我不知道用生成的 id 从数据库中删除值。 请帮忙! 表脚本:

<label>Your Order:</label>
        <table class="table table-dark table-striped">
            <thead class="thead-dark">
            <tr>
                <th>Name</th>
                <th>Order's List</th>
                <th>options</th>
                <th>delete</th>
            </tr>
            </thead>
            <tbody id="orderlist" class = "table=success">
            </tbody>
        </table>

脚本:

<script>

      let myFirebase = firebase.database();
      let dataRef = myFirebase.ref('/orders');

      function deleteRow(r) {

}

      dataRef.limitToLast(5).on('child_added', function(childSnapshot) {
      let item = childSnapshot.val()
      document.getElementById('orderlist').innerHTML += "<tr><td>" + item.student + "</td><td>" + item.food + "</td><td>" + item.options + "</td><td>" + "<button onclick=deleteRow(this)>"+"delete"+"</button></td></tr>"
});
    </script>

将函数更改为以下内容:

<button onclick=deleteRow(this,'"+ item.student + "')>

然后您可以执行以下操作:

function deleteRow(elem, student){
  let myFirebase = firebase.database();
  let dataRef = myFirebase.ref('/orders');
    dataRef.orderByChild("name").equalTo(student).on('value', function(snapshot){
      snapshot.forEach(function(childSnapshot) {
        let key = childSnapshot.key;
        dataRef.child(key).delete(); 
          });
    });
}

这里首先将变量item.student添加到 onclick,然后使用查询可以检索该特定studentkey并使用delete()方法delete() 您需要有以下数据库:

orders
   pushKey
      name      : "62070228"
      orderList : "Noodles"
      options   : "no vegetables"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM