javascript - 如何使用 HTML 表中的按钮删除 Firebase 数据库中的数据
问题描述
我是使用 Firebase 的 HTML 新手,在我的项目中,我必须使用 HTML 按钮从 Firebase 实时数据库中删除数据。下面是我的代码片段。在脚本部分,有一个 onclick 函数可以在单击时删除数据,但它没有显示任何更改。我需要从相应的按钮中删除行。如何实现它以及我应该使用哪些方法。
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Get a reference to the database service
var db = firebase.database();
// get Data
function SelectAllData(){
firebase.database().ref('+919512502625').child('S_complain').child('+917226097736').once('value',
function(AllRecords){
AllRecords.forEach(
function(CurrentRecord){
var title = CurrentRecord.val().C_title;
var Details = CurrentRecord.val().C_details;
var Date = CurrentRecord.val().C_date;
var Status = CurrentRecord.val().C_status;
var co_id = CurrentRecord.val().C_id;
AllItemsToTable(title, Details, Date, Status);
}
);
});
}
window.onload = SelectAllData;
// fill table
// var counter = 0;
function AllItemsToTable(title, Details, Date, Status){
var tbody = document.getElementById('tablebody');
var trow = document.createElement('tr');
var td1 = document.createElement('td');
//var check = document.createElement('input');
var td2 = document.createElement('td');
var td3 = document.createElement('td');
var td4 = document.createElement('td');
let del = document.createElement('button');
td1.innerHTML = title;
//check.innerHTML = 'checked';
td2.innerHTML = Details;
td3.innerHTML = Date;
td4.innerHTML = Status;
del.textContent = 'delete';
trow.appendChild(td1);
//trow.appendChild(check);
trow.appendChild(td2);
trow.appendChild(td3);
trow.appendChild(td4);
trow.appendChild(del);
tbody.appendChild(trow);
// Delete data
}
var index, table = document.getElementById('tableid');
for(var i = 0; i < table.rows.length; i++)
{
table.rows[i].cells[4].onclick = function()
{
index = this.rowIndex;
console.log(index);
};
}
document.getElementById('del').onclick = function(){
AllItemsToTable();
firebase.database().ref('+917226097736/'+co_id).remove();
}
</script>
</body>
</html>
请赐教。
解决方案
推荐阅读
- ubuntu - 我在并行积分计算程序中没有看到任何错误,但出现分段错误(代码 139),我该如何解决?
- angular - 扩展接口的可选泛型
- javascript - 通过 URL 传递参数将其显示为 [object object] 而不是字符串(打字稿)
- python - 修复损坏的 Anaconda 环境
- javascript - 如何从电子表格中获取数据并将其发送到 html 表格 googel script web
- python - 如果循环熊猫数据框字典
- c++ - QPushButton 在第二个布局中不可点击
- c# - 从阅读窗格中获取单击的超链接
- java - DBCC CHECKDB 未在 Java (MS SQL) 中返回结果集
- visual-studio - Visual Studio 2019 和经典 ASP:无法打开脚本文档