我正在做订单商店项目,当人们订购并添加到数据库时,订单列表页面与 firebase 中的实时数据库一起工作,订单将更新到订单列表表,如下所示:https://i.imgur.com/NjtYHSo.png 在删除按钮上,我想在 cooking 完成的食物时单击此按钮,订单食物将从数据库中删除。但我不知道从生成的 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,然后使用查询可以检索此特定 student
的 key
和使用方法 delete()
删除它。您需要有以下数据库:
orders
pushKey
name : "62070228"
orderList : "Noodles"
options : "no vegetables"
关于javascript - 单击按钮时 firebase 删除数据库上的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59298441/