这适用于将使用firebase进行调查的网络应用。我需要帮助的是,当应用程序将数据导出到表中时,它会抓取数据,但无法将其推送到表中,任何帮助都将受到赞赏。由于HTML代码很长,我只会放置表部分: HTML文件的表部分
<div id = "table">
<pre id = "snap-test"></pre>
<table id ="File-Table" class="table">
<thead>
<tr>
'<td><button onclick = "DeleteTabele()" id = "Delete-btn">Delete File</button></td>'
</tr>
</thead>
<button onclick ="Return()" id= "Log-btn" type="submit" class="btn btn-">Add a new File</button>
</table>
</div>
Table.js文件
var table = document.getElementById("File-Table");
const file = $("#File").val();
var requests = [];
function Export(){
//calls the file id in the HTML element
$("#Survey-Page").hide();
$("#File-Table").show();
$("#Log-btn").show();
var result = [];
//calls the database from the firebase known as users then using a function we nest a snapshot in it for later
firebase.database().ref('/users/').once('value').then(function(snapshot){
//if snapshot is empty then the window will alert
if (snapshot.val() == null){
alert("Does not exist");
}
// if the snapshot is full then it will genereate a table based on the snapshot value of the database
else {
console.log(snapshot.val());
let result = snapshot.val()
for(let k in result){
this.requests.push({
id: k,
value: result[k]
});
}
var MyTable = '<tr>' +
'<td>' + snapshot.val().txtName +'</td>' +
'<td>' + snapshot.val().txtEmail +'</td>' +
'<td>' + snapshot.val().FileName + '</th>' +
'<td><button id = "Email-btn">Send Survey</button></td>' +
'<td><button onclick = "DeleteTabele()" id = "Delete-btn">Delete File</button></td>' +
'</tr>';
MyTable += "</tr></table>";
table.innerHTML = MyTable;
}
console.log(snapshot.val());
});
答案 0 :(得分:0)
从您发布的代码中,更可能的原因是您的引用引用了多个用户的节点而不是特定用户。
firebase.database().ref('/users/')
要确认此假设,我们需要查看您的数据库结构。你可以编辑你的帖子吗?
然而,让我们想象这个假设是正确的。那你有两个解决方案:
如果要显示users
节点下的ONE用户的值,则必须更改引用并指向该用户,例如:
firebase.database()。ref(&#39; / users /&#39; + userID)
然后其余代码将正常工作
如果要在表中显示整个用户列表(逐行),则必须循环查询结果,如下所示:
firebase.database().ref('/so').once('value').then(function(snapshot){
var MyTable;
snapshot.forEach(function(childSnapshot) {
MyTable += '<tr>' +
'<td>' + childSnapshot.val().txtName +'</td>' +
'<td>' + childSnapshot.val().txtEmail +'</td>' +
// ...
'<td><button id = "Email-btn">Send Survey</button></td>' +
'<td><button onclick = "DeleteTabele()" id = "Delete-btn">Delete File</button></td>' +
'</tr>';
});
table.innerHTML = MyTable;
});
请参阅此处的文档:https://firebase.google.com/docs/database/web/lists-of-data#listen_for_value_events
此外,如果可以的话,你可以查看这篇SO帖子:HTML : draw table using innerHTML,其中显示了在&#34;简单&#34;中编写表格行的一些最佳实践。的JavaScript。